Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: this.setState is not a function

I see a number of questions on here relating to this same issue, but it seems none match the issue I'm having, and are a bit more complex.

I am in the process of learning ReactJS and React Native. I'm in the midst of reading and following the code examples from "Learning React Native" book here: https://github.com/bonniee/learning-react-native

For some reason, calling this.setState in the code below when the handleTextChange function is called, causes the "this.SetState is not a function." error. My question is why? Unlike other questions about this same issue, I don't believe my call to this.stateState is buried in a callback function or if statement. Why is it undefined?

Here is my code:

class WeatherProject extends Component {   constructor(props) {     super(props);     this.state = {       zip: "",       forecast: null     };   }    _handleTextChange(event) {     this.setState({zip: event.nativeEvent.text});   }      render() {     return (       <View style={styles.container}>       <Text style={styles.welcome}>       You input {this.state.zip}.       </Text>       <TextInput       style={styles.input}       onSubmitEditing={this._handleTextChange}/>       </View>     );   } } 
like image 860
Steed-Asprey Avatar asked Jul 18 '16 15:07

Steed-Asprey


People also ask

How do I fix setState is not a function?

There are two ways to fix this error: Declare the class method using the arrow function syntax. Bind the class method reference during constructor call or inside the calling property.

Is not function in react native?

The React. js "Uncaught TypeError: X is not a function" occurs when we try to call a value that is not a function as a function, e.g. calling the props object instead of a function. To solve the error, console. log the value you are calling and make sure it is a function.

Is setState a function?

The setState function takes an optional callback parameter that can be used to make updates after the state is changed. This function will get called once the state has been updated, and the callback will receive the updated value of the state.


1 Answers

Do not use bind inside a render. bind is a rather expensive operation and should only happen once. you have two options:

either bind the function in the constructor:

this._handleTextChange = this._handleTextChange.bind(this); 

or use arrow function:

onSubmitEditing={(e) => this._handleTextChange(e)} /> 

Edit

Apparently arrow functions inside render is also a bad practice (Thx to Adam Terlson in the comments and answer below). You can read eslint docs which states:

A bind call or arrow function in a JSX prop will create a brand new function on every single render. This is bad for performance, as it will result in the garbage collector being invoked way more than is necessary.

Using arrow functions is obviously not as bad as using bind, but nevertheless should be avoided.

like image 109
atlanteh Avatar answered Sep 19 '22 23:09

atlanteh