Jon
Jon

Reputation: 313

React : this.state.renderData.filter is not a function

I am working on a React project. I am using search filter in my project but in meantime when User type something in search box I am getting an error that this.state.renderData.filter is not a function. I am new to ReactJS.

Code

    class Example extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      Item: 5,
      skip: 0
    }

    this.handleClick = this.handleClick.bind(this);
  }

  urlParams() {
    return `http://localhost:3001/meetups?filter[limit]=${(this.state.Item)}&&filter[skip]=${this.state.skip}`
  }

  handleClick() {
    this.setState({skip: this.state.skip + 1})
  }

  render() {
    return (
      <div>
        <a href={this.urlParams()}>Example link</a>
        <pre>{this.urlParams()}</pre>
        <button onClick={this.handleClick}>Change link</button>
      </div>
    )
  }
}


ReactDOM.render(<Example/>, document.querySelector('div#my-example' ))

Upvotes: 0

Views: 1881

Answers (1)

philippbauch
philippbauch

Reputation: 61

The problem is that your initial state of

this.state = {
  renderData:[],
  ...
}

is overwritten when componentWillReceiveProps(nextProps, renderProps) {...} is called.

Consider the following call to connect(...):

connect(
  mapStateToProps,
  { getParties, searchData }
)(Organization)

The second argument to the connect() function - usually referred to as mapDispatchToProps, which is an object in your case - expects its attributes to be action creators (functions). The call to connect(...) will set your component's searchData prop to the value of the action creator that you import with import { getParties, searchData } from "../../actions";.

Because

componentWillReceiveProps(nextProps, renderProps) {
  ...
  this.setState({
    ...,
    renderData: nextProps.searchData
  });
  ...
}

is called before your component renders, the value of this.state.renderData won't be [], but an action creator and thus filter is not a function on your action creator.

Upvotes: 1

Related Questions