Answer the question
In order to leave comments, you need to log in
Clear input React/Redux?
Good afternoon! Please tell me how to clear the input in react / redux. Now when you click on the button, an entry is added and you need to clear the input. With props is cleared, but the value still remains in the input.
Here is a piece of code
// Компонент Input
handleChange(event) {
const {value} = event.target;
this.props.onChange(value);
this.setState({value});
}
render() {
return (
<input
type='text'
value={this.state.value}
onChange={this.handleChange}
className='form-control'
/>
);
// Родительский компонент
inputOnChange(value) {
this.setState({todoName: value});
}
addTodo() {
let {todos} = this.props.home;
const id = todos[todos.length - 1].id + 1;
const name = this.state.todoName;
this.props.dispatch(addTodo(id, name));
this.setState({todoName: ''});
// тут с переменной очищается, а инпут нет
}
render() {
const {todoName} = this.state;
const {todos, error} = this.props.home;
return (
<div className="container-fluid">
<div className="col-xs-4">
<Input onChange={this.inputOnChange} value={''} error={error}/>
<button className="btn btn-primary" onClick={this.addTodo}>Add todo</button>
</div>
</div>
);
}
Answer the question
In order to leave comments, you need to log in
In the Input component, you use state as the value of the input:
At the same time: you also use the state of the parent component and clear it:
addTodo() {
// ...
this.setState({todoName: ''});
}
Do you use lifecycle methods with redux (componentWillReceiveProps etc..)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question