Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 具有react redux connect和redux数据的组件生命周期顺序_Reactjs_React Native_Redux_React Redux - Fatal编程技术网

Reactjs 具有react redux connect和redux数据的组件生命周期顺序

Reactjs 具有react redux connect和redux数据的组件生命周期顺序,reactjs,react-native,redux,react-redux,Reactjs,React Native,Redux,React Redux,我们都知道constructor->componentWillMount->componentDidMount是执行顺序 现在,当redux开始发挥作用并尝试在组件生命周期中访问redux属性时。connect执行的顺序是什么,以便数据可用生命周期方法忽略并更新数据以重新使用。可能性是 1. Connect (DATA AVAILABLE) -> constructor & componentWillMount & componentDidMount 2. constru

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

现在,当redux开始发挥作用并尝试在组件生命周期中访问redux属性时。connect执行的顺序是什么,以便数据可用生命周期方法忽略并更新数据以重新使用。可能性是

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) 
顺序是否一致取决于加载的数据

react和react native


在PropType中将redux属性定义为所需,可以吗?初始执行顺序为-

连接(数据可用)->constructor&componentWillMount&Render&componentDidMount


当站点启动时,在组件装载生命周期之前,redux connect将首先使用其默认状态和操作初始化。但是,如果redux中有API调用,组件装载生命周期将不会等待数据。相反,如果组件已装入且redux返回数据,则将调用组件更新生命周期。

connect
是一个包装组件的HOC,因此组件生命周期方法位于连接生命周期之后。为了简单的理解,你可以这样写connect

const connect = (mapStateToProps, mapDispatchToProps) => (Component) => {
    return class ReduxApp extends React.Component {
        // lifecycle of connect
        render() {
            return (
                 <Component {...mapStateToProps(state)} />
            )
        }
    }
}
一旦数据更新

Connect (DATA AVAILABLE) -> componentWillReceiveProps/getDerivedStateFromProps -> componentWillUpdate -> render -> componentDidUpdate

你能提供你的用例来更好地理解问题吗?我希望这是一个一般的场景,没有任何特定的用例@abby37,所以回答你的答案,回答了我所有的问题。1.Connect(数据可用)->constructor&componentWillMount和componentDidMount的顺序为2。执行顺序是一致的。与react和react native 4相同。是的,可以在PropType中为redux属性定义required
Connect (DATA AVAILABLE) -> componentWillReceiveProps/getDerivedStateFromProps -> componentWillUpdate -> render -> componentDidUpdate