I am pulling an array of objects from an external API. I am taking this array of objects called trails and using .filter() to filter the objects based on a input field, userInput. Here's what the code looks like:
onChange = (e) => {
let trails = this.props.trails;
const userInput = e.currentTarget.value;
const filteredTrails = trails.filter(
(obj) => obj.name.toLowerCase().indexOf(userInput.toLowerCase()) > -1);
console.log(userInput);
console.log(filteredTrails);
this.props.searchTrails(this.state.filteredTrails);
this.setState({
userInput,
});
};
As you can see, I pass filteredTrails as props to the searchTrails method in my App level component. It looks like this:
const searchTrails = (filteredTrails) => {
setTrails(filteredTrails);
console.log(filteredTrails);
};
My issue is that the filter only works one way.
If the user wanted to hit backspace for instance, how do I add trails back to this filteredTrails array of objects?
Thanks in advance!