currently trying to use setState to clear a controlled input,
adding a value to the input field and using setState to clear it
The function that clears the state
createNewBoard = (e) => {
e.preventDefault();
const newItem = { boardname: this.state.currentValue, todo: []};
const updatedList = [...this.state.boards, newItem]
this.setState({
boards: updatedList,
currentValue: ''
});
}
passing down the props to the component
render() {
return(
<>
{ this.state.isBoardOpen ?
<ActiveCreateBoard
toggleCreateBoard={this.toggleCreateBoard}
onChange={this.onChange}
currentValue={this.currentValue}
createNewBoard={this.createNewBoard}
/> : <CreateBoard toggleCreateBoard={this.toggleCreateBoard}/> }
<ShowAllBoards boards={this.state.boards}/>
</>
)
}
the component itself which is not updating the input field
const ActiveCreateBoard = ({ toggleCreateBoard, onChange, currentValue, createNewBoard }) => {
return(
<div className="card">
<div className="card-body">
<form onSubmit={createNewBoard}>
<h4>Pick a board name</h4>
<input
type="text"
onChange={onChange}
value={currentValue}/>
</form>
</div>
<button onClick={toggleCreateBoard}>close</button>
</div>
)
}
this.setState is clearing the currentValue in state, but the value of the input field is not reflecting the update in state
render()that setscurrentValue. - Chris GcreateNewBoardandonChangefromthisandcurrentValuefromthis.state. If possible, please share your whole component here (as a working copy). - devserkan