TIL_180712 ReactJS Component (cycle, smart/dumb)
ReactJS
Order in component cycle
- componentWillMount(): request api
- render()
- componentDidMount()
Update stage
- componentWillReceiveProps() -
- shouldComponentUpdate(): comparison in new prop and old prop, if have changes, == true
- componenetWillUpdate()
- render()
- componentDidUpdate()
Data type checking
|
|
State
- state = {}
- if ‘state’ change, component do ‘render()’ again automatically
- when want to change the state’s value, should use this.setState({content})
- can’t over ride like ‘this.state.varName = ‘other value’ -> automatic change ain’t be happend.
- TIP for infinite scroll
Smart Componenet vs Dumb Componenet
- if component doesn’t need to have componenetWillMount, function, updateState, render but only do ‘return’, it can be stateless componenet (= Dumb Componenet).
- use ‘functional component’ instead of ‘class componenet’
- for checking prop type: use the name instead of ‘static’ keyworkd.
- eg) MoviePoster.proptypes = { poster: PropTypes.string.isRequired}
How to change smart component into dumb.
|
|