Undefined is not an object in React Native

2496 views react-native

Hi ninjas quick question I have a button which runs the following


but if I change the code to have a function:

closeModal() { this.props.productStore.closeModal; //alert('close modal'); }

and the button trigger onPress={this.closeModal} I get undefined is not an object. (this.props.productStore.closeModal is a mobx store which updates isModalVisible to false). If I uncomment alert in closeModal function the alert runs fine. Any help with resolving is appreciated. Thanks in advance

answered question

1 Answer


In the second variant, this.props is undefined because the function closeModal is not bound to this. Use either the arrow function syntax like below:

closeModal = () => {

or bind the function to this.


Another problem that I noticed in the code is you are not calling the function. You should call the function.


posted this

Have an answer?


Please login first before posting an answer.