Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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 将Redux存储数据作为道具从parent1传递->;家长2->;子(或)使用mapToStateProps直接在子组件中调用redux存储?_Reactjs_React Redux_React Props - Fatal编程技术网

Reactjs 将Redux存储数据作为道具从parent1传递->;家长2->;子(或)使用mapToStateProps直接在子组件中调用redux存储?

Reactjs 将Redux存储数据作为道具从parent1传递->;家长2->;子(或)使用mapToStateProps直接在子组件中调用redux存储?,reactjs,react-redux,react-props,Reactjs,React Redux,React Props,将Redux存储数据作为道具从父组件(它已经被用于某些其他功能)传递到子组件比直接在子组件中调用Redux存储好吗 我尝试了传递道具和直接调用子组件。在功能上找不到太多差异,只是想知道标准做法是什么,以及它如何影响巨大数据的性能 ExampleReduxdata: {'Apple', 'Banana', 'Mango', 'Orange'} class ParentComponent1 extends React.Component { render(){ retu

将Redux存储数据作为道具从父组件(它已经被用于某些其他功能)传递到子组件比直接在子组件中调用Redux存储好吗

我尝试了传递道具和直接调用子组件。在功能上找不到太多差异,只是想知道标准做法是什么,以及它如何影响巨大数据的性能

ExampleReduxdata: {'Apple', 'Banana', 'Mango', 'Orange'}

    class ParentComponent1 extends React.Component {

    render(){
     return (<ParentComponent2 reducerData={this.props.reducerData} />
    }

    const mapStateToProps = {

    reducerData: state.ExampleReduxdata,
     }
    }
    class ParentComponent2 extends React.Component{
    render(){
     return (<ChildComponent reducerData={this.props.reducerData} />
     }

    }
    (OR)


    class ChildComponent extends React.Component{

    render(){
     return (
        <div>{this.props.reducerData}</div>
       );
    }

    const mapStateToProps = {

    reducerData: state.ExampleReduxdata,
    } 

    }
ExampleDuxData:{'Apple','Banana','Mango','Orange'}
类ParentComponent1扩展了React.Component{
render(){
返回(
}
常量MapStateTops={
reducerData:state.ExampleReduxdata,
}
}
类ParentComponent2扩展了React.Component{
render(){
返回(
}
}
(或)
类ChildComponent扩展了React.Component{
render(){
返回(
{this.props.reducerData}
);
}
常量MapStateTops={
reducerData:state.ExampleReduxdata,
} 
}

基于主要的Redux文档:

React绑定用于Redux将表示组件与容器组件分开。这种方法可以使您的应用程序更易于理解,并允许您更轻松地重用组件

因此,我认为为了使组件可重用并遵循单一真实源原则,最好使用
connect
方法包装组件


另一种避免在功能组件中使用这些方法和包装的简单方法是redux hooks
useSelector
useDispatch

不同之处在于,当传递的道具发生变化时,哪些组件将重新渲染。@BugsArePeopleToo谢谢!@Domino987-非常感谢!!!这为我清理了一切