5

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

2 Answers 2

3

you need to change the FilterInput onChange= handler on the input:

<input onChange={event => this.props.onChange(event.target.value)} {...otherProps} />

obviously, design an APi contract between the two where you either pass the value or the event raw to the onChange handler and in the parent make sure you have:

<FilterInput onChange={this.updatefilters.bind(i)} />

make sure the updatefilters is bound to this first in your constructor.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, by this way I'm getting the value but I lost the index of the input <:( and it's important for me to track the value of the index!!
I answer myself, I just added a new parameter in updateFilters function and pass it inside FilterInput onChange handler. <input type='text' className="form-control" defaultValue={this.props.filterApplied} onChange={event => this.props.onChange(event.target.value, this.props.keyField)} /> and the function: updateFilters(val, i) { ..... }
1

Change the onChange as so:

filters.push(<FilterInput key={i} filterApplied={this.state.inputs[i]} onChange={(evt) => this.updateFilters(i, evt)} />);

Then change your updateFilters to get the value from the event now passed in:

updateFilters(i, evt) {
    const value = evt.target.value;
    ....
}

1 Comment

By this way I'm getting Uncaught TypeError: Cannot read property 'target' of undefined in the function updateFilters. It seems not be working for me. Thank you anyways!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.