Class Based | Functional | |
產生組件 | O, 倚賴 extends Component | X, 用 ES6閉包, const ABC = props => { ... } |
存取 state | this.state, this.setState({ abc: 123 }) | const [abcState, setAbcState] = useSate(123) |
LifeCycle Hook | O, ex: update hooks http://t.ly/G5RL | X |
存取 props | this.props | props |
* 自動 merge state | this.setState({ name: 'leo' }) this.setState({ age: 38 }) state => { name: 'leo', age: 38 } | const [nameState, setNameState] = useSate('leo') => { name: 'leo' } const [ageState, setAgeState] = useSate(38) => { age: 38 } |
使用時機 | 要 mergeState 要 Lifesyscle Hooks 不用 ReactHooks (ex:useState) | 沒什麼限制 |
若有寫錯還請大大通知,感激~
留言
張貼留言