亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 ReactJs 中單擊按鈕時顯示“無法讀取未定義的屬性編輯名稱”

在 ReactJs 中單擊按鈕時顯示“無法讀取未定義的屬性編輯名稱”

哈士奇WWW 2022-01-07 10:42:17
我正在嘗試編輯表格中的值并將其放入文本框中。當我單擊編輯按鈕時,它顯示“無法讀取未定義的屬性編輯名稱”。我使用了胖箭頭功能。我也在構造函數中使用了綁定,但它有同樣的錯誤。以下是我的代碼。單擊editName按鈕時,它會出錯。class App extends React.Component {  constructor(props) {    super(props);    this.onNameChange = this.onNameChange.bind(this);    this.onSurnameChange = this.onSurnameChange.bind(this);    this.onIdChange = this.onIdChange.bind(this);    this.editName = this.editName.bind(this);    this.state = {      data: "",      name: "",      surname: "",      id: ""    };  }  componentDidMount() {    axios.get("http://localhost:4000/employees").then((response, err) => {      if (err) {        console.log("err");      }      this.setState(prevstate => ({        data: response.data      }));    });  }  handleSumbit(e) {    axios      .post("http://localhost:4000/employees", {        name: this.state.name,        surname: this.state.surname,        id: this.state.id      })      .then((response, err) => {        if (err) {          console.log("Error While Posting Data", err);        }        console.log("RESPONSE FROM POST", response);      });  }  onNameChange(e) {    this.setState({      name: e.target.value    });  }  onSurnameChange(e) {    this.setState({      surname: e.target.value    });  }  onIdChange(e) {    this.setState({      id: e.target.value    });  }  editName(value) {    this.setState({      name: value    });  }  editSurname(e, value) {    this.setState({      surname: value    });  }  render() {    const { data } = this.state;    return (      <div className="container">        <div>          <label className="">Name</label>          <input            type="text"            name=""            value={this.state.name}            onChange={e => this.onNameChange(e)}          />        </div>
查看完整描述

1 回答

?
呼如林

TA貢獻1798條經驗 獲得超3個贊

我注意到(正如@DanO 所說)this不是窗口對象,而是在渲染方法中undefined使用內部地圖function時。解決方案非常簡單,將其更改為箭頭函數(首選)或使用Function.prototype.bind.

data.map((data, key) => (<>...</>))


查看完整回答
反對 回復 2022-01-07
  • 1 回答
  • 0 關注
  • 120 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號