0

I'm trying to make a basic app by fetching data from an API. I'm now trying to filter a value from that data through an array. I'm using react-search-input and getting "TypeError: Cannot read property 'filter' of undefined" and I don't understand why.

What do I need to do to avoid this error ?

Code here :

import React, { Component } from 'react';
import SearchInput, {createFilter} from 'react-search-input'

const API = 'https://swapi.co/api/people/';

class App extends Component {
    constructor(props) {
    super(props);

    this.state = {
      items: [],
      searchTerm: '',
    }
    this.searchUpdated = this.searchUpdated.bind(this)
  }

  componentWillMount() {
    this.fetchData();
  }

  fetchData = async () => {
    const response = await fetch(API);
    const json = await response.json();
    this.setState({ items: json.results })
  }

  render() {
    const items = this.state.items;
    const filteredData = items.results.filter(createFilter(this.state.searchTerm, items.results))

    return (
      <div>
        <SearchInput className="search-input" onChange={this.searchUpdated} />
        <div className="row">
            {filteredData.map((item, i) =>
              <div className="col-md-4" key={i}>
                <a href={item.url}>{item.name}</a>
                <p>Caractéristiques :</p>
                <ul>
                  <li>Année de naissance : {item.birth_year}</li>
                  <li>Taille : {item.height} cm</li>
                  <li>Masse : {item.mass}</li>
                  <li>Couleur des yeux : {item.eye_color}</li>
                  <li>Couleur de cheveux : {item.hair_color}</li>
                  <li>Couleur de peau : {item.skin_color}</li>
                </ul>
              </div>
            )}
        </div>
      </div>
    )
  }
  searchUpdated (term) {
    this.setState({searchTerm: term})
  }
}

export default App;
1
  • item.results should be an array but items is declared to [] in your state Commented Apr 3, 2019 at 21:36

2 Answers 2

1

You are trying to access the results property on this.state.items which is declared as an empty array. You should declare items like this:

this.state = {
  items: { results: [] },
  searchTerm: '',
}
...
const items = this.state.items;
items.results.filter(createFilter(this.state.searchTerm, items.results))

or simply declare results as an array and use it:

this.state = {
  results: [],
  searchTerm: '',
}
...
this.state.results.filter(createFilter(this.state.searchTerm, this.state.results))
Sign up to request clarification or add additional context in comments.

Comments

0

You have a syntax error. You're trying to access a result key on an array.:

// items is an array, arrays don't have keyword result
const filteredData = items.results.filter(createFilter(this.state.searchTerm, items.results)) 

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.