tableflip / react-native-toaster

Simple top-pop toast feedback messages for React Native, also Redux compatible
ISC License
85 stars 29 forks source link

Warning message when trying execute onHide on unmounted component #11

Open caiorios opened 6 years ago

caiorios commented 6 years ago

Steps to reproduce the issue:

  1. Create a toaster with state inside a simple View.
  2. Run an event (clicking on a button for example).
  3. On event created, set the state with the desired message.
  4. After the message shows up on screen, exit the component (back button on navigator for example).
  5. After a while, a warning message shows up:

Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.

Example code to reproduce the issue:

constructor(props) {
    super(props);
    this.state = {
        toast: {}
    };
}
submitAction() {
    if (this.formIsNotValid) {
        this.setState({
            toast: {
              text: 'The form is not valid!',
              styles: ToastStyles.error
            }
          });
    }
}
renderMessage() {
    if (!_.isEmpty(this.state.toast)) {
        return <Toaster message={this.state.toast} />;
    }
    return <View />;
}
render() {
    return (
        <View>
            {this.renderMessage()}
            <TouchableOpacity
                style={s.buttonContainer}
                onPress={() => this.submitAction()}>
                <Text style={s.button}>Submit</Text>
            </TouchableOpacity>
        </View>
    )
}

package.json:

"dependencies": {
    "lodash": "^4.17.4",
    "moment": "^2.18.1",
    "react": "16.0.0-alpha.12",
    "react-native": "0.48.2",
    "react-native-camera": "^0.10.0",
    "react-native-config": "^0.7.0",
    "react-native-datepicker": "^1.6.0",
    "react-native-fs": "^2.8.1",
    "react-native-htmlview": "^0.12.0",
    "react-native-image-gallery": "^2.0.0",
    "react-native-navigation": "^1.1.228",
    "react-native-tab-view": "0.0.69",
    "react-native-toaster": "^1.1.2",
    "react-native-vector-icons": "^4.3.0",
    "react-redux": "^5.0.6",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0"
  }