0

I am trying to have a search bar which updates the set of json entries depending upon query. Here's the code that displays the video list (<Videos videos={this.state.data}/>). When the page loads, I want to call <Videos videos={this.state.data}/>, but after this query from search bar should update this list. My search functionality is not working for some reason.

class App extends Component {
  state = {
    query: "",
    data: [],
    filteredData: []
  };

  handleInputChange = event => {
    const query = event.target.value;

    this.setState(prevState => {
      const filteredData = prevState.data.filter(element => {
        return element.title.toLowerCase().includes(query.toLowerCase());
      });

      return {
        query,
        filteredData
      };
    });
  };

  getData = () => {
    fetch('http://localhost:3000/api/videos')
      .then(response => response.json())
      .then(data => {
        const { query } = this.state;
        const filteredData = data.filter(element => {
          return element.title.toLowerCase().includes(query.toLowerCase());
        });
        this.setState({
          data,
          filteredData
        });
      });
  };

componentWillMount() {
    this.getData();
  }

  render() {
    return (
      <div className="searchForm">
        <form>
          <input
            placeholder="Search for..."
            value={this.state.query}
            onChange={this.handleInputChange}
          />
        </form>
        <Videos videos={this.state.data}/>
        <div>{this.state.filteredData.map(i => <p>{i.name}</p>)}</div>
      </div>
    );
  }
}

I am new to react, any pointers will be appreciated.

3 Answers 3

1

You are creating a new object without the data property and setting that object as the state whenever you are calling this.setState. So the data property is getting deleted.

On the handleInputChange method do this:

 return {
       ...this.state,
        query,
        filteredData
 };

And on the getData method do this:

 this.setState({
          ...this.state,
          data,
          filteredData
 });

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

2 Comments

He doesn't mutate the state. He creates a new object for the state but without ...this.state
You're right. I updated the answer. Thanks @RukkiesMan.
0

You are using this.state.data in the Videos component which you got from the server. You should use this.state.filteredData to show entries depend on query:

<Videos videos={this.state.filteredData}/>

1 Comment

Thanks. Your suggestion along with EVeras helped. I can only mark one answer as solved.
0

You need to bind the handleInputChange to this. Look at this for more on this.

Comments

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.