this is my code...
class Module extends Component {
constructor() {
super()
this.state = {
inputs: [
{ type: 'text', placeholder: 'placeholder text', name: 'text1', id: 'text1', value: 'aaa' },
{ type: 'text', placeholder: 'another placeholder text', name: 'text2', id: 'text2', value: '' },
{ type: 'text', placeholder: 'third placeholder text', name: 'text3', id: 'text3', value: '' },
]
}
this.handleInputChange = this.handleInputChange.bind(this)
this.saveModule = this.saveModule.bind(this)
}
handleInputChange(event) {
this.setState ({
[event.target.name]: event.target.value
})
}
renderInput = (input) => {
return(
<div key={ input.id }>
<input
type={ input.type }
name={ input.name }
placeholder={ input.placeholder }
onBlur={ this.saveModule }
value={ input.value }
onChange={ this.handleInputChange }
/>
</div>
)
}
render() {
return (
<div>
{ this.state.inputs.map(this.renderInput) }
</div>
)
}
}
export default Module
How can i handle change of input which value is rendered from state in this way?! If i had {this.state.input.value} it works perfectly fine, once I refactor it like this, the setState doesn't seem to reach it anymore.
Any ideas? :)
Thanks a lot in advance!
event.target.nameas the condition to filter the array. then create a new array with the changes. and set the state. heres a good way how to do it stackoverflow.com/a/36010124/7744070