import React from "react";
export default class App extends React.Component {
state = {
items: [],
text: ""
};
handleTextChange = event => {
this.setState({
text: event.target.value
});
};
render() {
return (
<div className="appMain">
<form onSubmit={this.addItem}>
<input
placeholder="Enter task: "
onChange={this.handleTextChange}
value={this.state.text}
/>
<button type="submit">Add</button>
</form>
</div>
);
}
}
Hey, I'm new to react and can't solve my problem.
Why doesn't my handleTextChange change the state of text straight away? Every time I console.log the state of 'text' it is always one behind what it should be. For example when I enter 'a' into the text field, state.text is "". Then when I enter 'b' and view state.text, it is 'a'. And so on and so forth.
Thanks