I'm learning React and I'm building a table component with inputs to filter the data. My structure is:
- FilterBar
- FilterInput
I want my FilterBar able to store the FilterInput values in the class state in this way ==> this.state = {inputsVal: ["input1value", "input2value", "", ...]}.
I dynamic build an input component for each header (attribute in my database). So when I write in one of those components i want to capture the new value and the position of this component in order to refresh the FilterBar state array. I success on getting the position(index) of the component but not the value. I dont know how to achieve this. Here are my components:
class FilterBar extends React.Component {
constructor(props) {
super(props);
this.state = {
inputs: Array(props.headers.length).fill("")
};
this.updateFilters = this.updateFilters.bind(this);
}
updateFilters(i) {
console.log(i); //==>I can see the index of the input when it changesbut i want also to see the new value
}
renderFilterInputs() {
var filters = []
for (let i=0; i < this.state.inputs.length; i++) {
filters.push(<FilterInput key={i} filterApplied={this.state.inputs[i]} onChange={() => this.updateFilters(i)} />);
}
return filters;
}
render() {
return (
<tr className="filters-bar">
{this.renderFilterInputs()}
</tr>
);
}
}
and here is FilterInput component
class FilterInput extends React.Component {
render() {
return (
<td>
<input type='text' className="form-control" defaultValue={this.props.filterApplied} onChange={() => this.props.onChange()} />
</td>
);
}
}
Thank you