使用react-redux连接和redux数据的组件生命周期顺序

Gau*_*jan 10 reactjs react-native redux react-redux

我们都知道这constructor -> componentWillMount -> componentDidMount是执行的顺序.

现在,当redux发挥作用并尝试在组件生命周期中访问redux属性时.什么是该命令连接将执行,这样数据可用的生命周期方法忽略和数据更新用,以终极版.可能性是

1. Connect (DATA AVAILABLE) -> constructor & componentWillMount & componentDidMount

2. constructor -> Connect (DATA AVAILABLE) -> componentWillMount & componentDidMount

3. constructor -> componentWillMount -> Connect (DATA AVAILABLE) -> componentDidMount

4. constructor -> componentWillMount -> componentDidMount -> Connect (DATA AVAILABLE) 
Run Code Online (Sandbox Code Playgroud)

并且订单是否一致取决于加载的数据?

反应和原生反应是不同的

并且可以根据PropType中的要求定义redux属性

Shu*_*tri 9

connect是一个包装组件的HOC,因此组件生命周期方法在连接生命周期之后.为了简单理解,您可以将连接写成这样写

const connect = (mapStateToProps, mapDispatchToProps) => (Component) => {
    return class ReduxApp extends React.Component {
        // lifecycle of connect
        render() {
            return (
                 <Component {...mapStateToProps(state)} />
            )
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,只要你的状态更新,connect就会浅显比较要返回给Component的道具列表,如果有更新,则更新道具,之后组件生命周期方法就像一个prop一样运行.

简而言之,最初的执行是

Connect (DATA AVAILABLE) -> constructor & componentWillMount & componentDidMount
Run Code Online (Sandbox Code Playgroud)

一旦数据更新

Connect (DATA AVAILABLE) -> componentWillReceiveProps/getDerivedStateFromProps -> componentWillUpdate -> render -> componentDidUpdate
Run Code Online (Sandbox Code Playgroud)