我正在嘗試編輯表格中的值并將其放入文本框中。當我單擊編輯按鈕時,它顯示“無法讀取未定義的屬性編輯名稱”。我使用了胖箭頭功能。我也在構造函數中使用了綁定,但它有同樣的錯誤。以下是我的代碼。單擊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>
在 ReactJs 中單擊按鈕時顯示“無法讀取未定義的屬性編輯名稱”
哈士奇WWW
2022-01-07 10:42:17