1

I want to search for people. An array of data is stored in the redux store. I'm trying to perform a search, but an error occurs.

Cannot read property 'searchInput' of undefined

What could be the problem?

searchHandler(){
  console.log('findUser', this.searchInput.value);
  this.props.onFindPeople(this.searchInput.value)
}

I add an example code

4
  • I'm afraid you'll have to post all the relevant code, i.e. component, storeToProps, reducer, etc. Commented Sep 30, 2018 at 19:16
  • @SebastianRothbucher he has it posted in the code link Commented Sep 30, 2018 at 19:18
  • @max are you doing binding of searchHandler in constructor? Commented Sep 30, 2018 at 19:19
  • the function was decided by the, thanks Commented Sep 30, 2018 at 19:25

2 Answers 2

2

You lost the context of this to the event handler.
You can either use an arrow function (that uses a lexical context for this):

  searchHandler = () => {
    console.log("findUser", this.searchInput.value);
    this.props.onFindPeople(this.searchInput.value);
  }

Or bind it to the class like this:

  constructor(props){
    super(props);
    this.searchHandler = this.searchHandler.bind(this);
  }

We do it in the constructor because it will run only once.

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

6 Comments

I checked the OP's snippet, this is the problem and arrow function or bind fixes it
you're the best, thank you! And it is possible still a question?
I want to click on the user, display more information about him on the right. I tried to withdraw, but all are output at once? Can you help me?
I think its better to close this question (if you think you got a valid answer) and open a new one as its not related to the same issue here and may confuse future readers. you can post a link to the new one as a comment and i'll try to answer it (or someone else)
I can ask the next question in 90 minutes, unfortunately.
|
1

You are not binding the function. You need to bind it in constructor like below. Binding is required in order to access this and access state, props and modifying the state.

Please note you need to bind it only in constructor not anywhere else in the component

 constructor(props){
      super(props);
      this.searchHandler = this.searchHandler.bind(this);
 }

 searchHandler(){
     console.log('findUser', this.searchInput.value);
     this.props.onFindPeople(this.searchInput.value);
 }

Or you can also use arrow function as Sagiv mentioned in his answer.

2 Comments

thanks, but there is an easy way to perform searches not only by name, but by other parameters? this is another matter
Yes. If you are looking for more details then you can put that as a separate question about the issue

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.