【发布时间】:2021-03-26 08:29:31
【问题描述】:
我在 ReactJS 中制作了一个带有一个文本输入的表单,当它提交时,我想获取它的值并将其放入一个变量中。但是当我使用 console.log() 时,它会以undefined 的形式返回。我该如何解决?这是我的代码。
class App extends Component {
state = {
todoTitle: ""
};
render() {
return (
<div>
<center>
<form
onSubmit={(event) => {
event.preventDefault();
this.setState(todoTitle: event.target.value,);
console.log(this.state.todoTitle); // Returns "undefined"
}}
>
<input
type="text"
autocomplete="off"
class="form-control"
name="todoInput"
placeholder="Enter todo"
style={{ width: "400px", height: "50px" }}
/>
<input
type="submit"
value="Submit"
id="submitButton"
></input>
</form>
</center>
}
}
}
【问题讨论】:
-
setState应该类似于this.setState({todoTitle: event.target.value}); -
@ani:您需要任何其他有关 ReactJS、Hooks、异步 javascript 的帮助,请随时向 emmeiwhite@gmail.com 发送消息或写电子邮件。快乐编码。如果有用,请给答案评分:) 谢谢
标签: javascript node.js reactjs