Vue triggers component re-render by it's reactivity mechanism, so developers can mutate states directly. Vue will detect the state change and trigger component rerender.
React triggers component re-render by manually setState
and React will diff VDOM to check if it should re-render or not. Developers are recommended not to mutate state directly but create new one to replace original state so the original and new states can be shallowEqual
efficiently.(immutable approach).
For example, there's a state as follows:
state = { a: 1, b: 2 }
// mutate state in Vue
state.a = 3
// mutate state in React
this.setState({...state, a: 3 })
It seems that the reactivity mechanism is more intuitive and can write less code. I wonder what's the pros and cons between these two approaches? What scenario should I choose one over another?