Last active
February 27, 2018 09:50
-
-
Save SuperAL/5f16449dce700998e2149ae8d34a706f to your computer and use it in GitHub Desktop.
Simple Notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyComp extends Component { | |
| // default state | |
| state = { fetching: 0 } | |
| componentWillMount() { | |
| // Setting state here won't trigger re-render | |
| this.setState({ fetching: 1 }); | |
| } | |
| componentDidMount() { | |
| // fetching data | |
| dispatch(yourAction()).then(res => { | |
| // Setting state in this method will trigger a re-rendering. | |
| this.setState({ fetching: 0 }); | |
| }); | |
| // It's fine to setState here if you need to access | |
| // the rendered DOM, or alternatively you can use the ref functions | |
| } | |
| render() { | |
| if (this.state.fetching) return <Loader /> | |
| return ( | |
| <div> // your data are loaded </div> | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment