0

I've tried to console.log the value of the text input but I get the error "undefined is not an object (evaluating 'this.state.inputValue')". What's the problem? Thank you!

class SearchScreen extends React.Component {
  state = {
    inputValue: "",
  };

  search() {
    console.log(this.state.inputValue);
  }

  render() {
    return (
          <View>
            <TextInput
              onChangeText={
                ((inputValue) => this.setState({ inputValue }),
                this.search)
              }
              value={this.state.inputValue}
            />
          </View>
    );
  }
}
export default SearchScreen;

4 Answers 4

1

The problem is in the way you've implemented it. Please try as below...

class SearchScreen extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      inputValue: '',
    };
  }

  search() {
    console.log(this.state.inputValue);
  }

  render() {
    return (
      <View>
        <TextInput
          onChangeText={(inputValue) => {
            this.setState({ inputValue });
            this.search();
          }}
          value={this.state.inputValue}
        />
      </View>
    );
  }
}

export default SearchScreen;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it works. One last question: if I write in the input "hello" the console retrieves "hell" (the last word is always missing unless I type something else or click the spacebar). Do you know why?
@Unaidu Yes, that's because this.search() gives the result BEFORE the state value is updated. It takes a little time to update the state. (Microseconds) But, the search function is executed before the state is updated. So, you always see until one before last letter in your console. But, you don't need to worry. The state value is already updated.
1

This problem occurred because two things.

First:

The this.setState is a async function.

If you pass a function after the setState this will work like a .then() in a promisse.

Second:

If you pass one function after another separating them by ',' the rightmost function will be executed first

You can resolve this doing something like that:

onChange={ inputValue => {
  this.setState({ inputValue });
  this.search();
}} 

Or you can try something like that:

class SearchScreen extends React.Component {
  state = {
    inputValue: "",
  };

  search = () {
    console.log(this.state.inputValue);
  }

  setSearch = inputValue => {
    // The function 'search' will be execute after the state was set

    this.setState(
     { inputValue },
     () => this.search()
    );

  }

  render() {
    return (
          <View>
            <TextInput
              onChangeText={ inputValue => this.setSearch(inputValue) }
              value={this.state.inputValue}
            />
          </View>
    );
  }
}
export default SearchScreen;

Comments

0

You didn't set the value of state property. provide a value to setState. this.setState({property : value})

1 Comment

You can use that syntax if the variable has the same name as the property, see here on 'New notations in ECMAScript 2015'
0

The problem is this line:

onChangeText={
    ((inputValue) => this.setState({ inputValue }),
    this.search)
}

You can use the short function notation only when your function has one statement:

(inputValue) => this.setState({ inputValue })

You actualy have 2 statements though, so you need to create a full function block using {}

(inputValue) => {
    this.setState({ inputValue })
    this.search()
}

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.