Hi,
I am new to React and I am trying to make a simple app with the concepts I learned.
I have created some counters components and I would like to search by their names.
The issue I am trying to solve is that ‘this.setState’ doesn’t reflect to changes as ‘console.log’. (Please, check out my idea and code below.)
Thank you,
Enric
// App.js
<Counters
counters={this.state.counters}
onSearch={this.handleSearch}
/>
// Counters.js
<input
type="search"
onInput={this.props.onSearch}
placeholder="Search Counter..."
/>
// App.js
handleSearch = e => {
const counters = this.state.counters.filter(counter =>
("counter #" + counter.id).includes(
e.target.value.trim().toLowerCase()
)
);
console.log(counters); // logs the correct result
// this.setState({ counters });
};