safe-state

1.0.4 • Public • Published

React Native wrapper for setState to prevent state manipulation on unmounted components.

How to use safe-state

//1. Import safe-state
import SafeState from 'safe-state';

//2. Initialize the safe-state object
this.setMountedState = SafeState(this)

//3. Use safe-state
this.setState({}) //instead of this
this.setMountedState({}) //use this

//Note: Lifecyle methods
componentDidMount(){...} //instead of this
componentDidMountSafely(){...} //use this

componentWillUnmount(){...} //instead of this
componentWillUnmountSafely(){...} //use this

A working react-native example can be found here.

Package Sidebar

Install

npm i safe-state

Weekly Downloads

5

Version

1.0.4

License

ISC

Unpacked Size

2.34 kB

Total Files

3

Last publish

Collaborators

  • kamranf