简体   繁体   English

如何将状态传递给 redux 中的多个组件

[英]How to pass state to more then one component in redux

I start working with React + Redux and newbie to the framework.*我开始使用 React + Redux 和框架的新手。*

What is am trying to do?我想做什么?

I have to click a button and on the click it will show an svg path in other container/svg tag.我必须单击一个按钮,然后单击它会在其他容器/svg 标签中显示一个 svg 路径。

My code or my code我的代码或我的代码

import { connect } from "react-redux";
import { BodyTemplate, Button } from "../component/svgPreview/Preview.jsx";
import { previewBodyTemplate } from "../modcon/Actions.js";

const mapStateToProps = ({ previewTemplateState }) => {
    return ({
        previewTemplateState: previewTemplateState
    });
};

const mapDispatchToProps = (dispatch) => {
    return ({
        onImageClick: (value) => {
            dispatch(previewBodyTemplate(value));
        }
    });
};

/*  the following code will work because we cannot export more then one by
default **/

/*
const PreviewContainer = connect(mapStateToProps, mapDispatchToProps)(Button);
const PreviewBody = connect(mapStateToProps, mapDispatchToProps)(BodyTemplate);
export default PreviewContainer;
export default PreviewBody;

*/


/* so i try this */

export const PreviewContainer = connect(mapStateToProps, mapDispatchToProps)(Button);
export const PreviewBody = connect(mapStateToProps)(BodyTemplate);

According to my knowlage i am passing the state to two components so when the state of one update it will update the other.根据我的知识,我将状态传递给两个组件,因此当一个组件的状态更新时,它将更新另一个组件。

But the file is not working because we and not export it directly.但是该文件不起作用,因为我们并没有直接导出它。

How i have to tackle to pass the state to more then one components我必须如何处理才能将状态传递给多个组件

below is the error when i am exporting directly以下是我直接导出时的错误

在此处输入图片说明 * *

You need to create a parent component, this component will contain the button and the preview component.您需要创建一个父组件,该组件将包含按钮和预览组件。

const MyContainer = ({ image, onImageClick }) => (
  <div>
    <div className="sidebar">
      <Button label="Preview image" onClick={onImageClick} />
    </div>
    <div className="content">
      <Preview source={image} />
    </div>
  </div>
);

Once you have your container ready, you need to map the props of this component to the state/actions from redux.准备好容器后,您需要将此组件的 props 映射到来自 redux 的状态/操作。

const mapStateToProps = ({ image, previewTemplateState }) => {
  return ({
    image: image,
    previewTemplateState: previewTemplateState
  });
};

const mapDispatchToProps = (dispatch) => {
  return ({
    onImageClick: (value) => {
        dispatch(previewBodyTemplate(value));
    }
  });
};

export default connect(mapStateToProps, mapDispatchToProps)(MyContainer);

Now the main container will receive the data and the actions, from there you can send whatever you need to the children components.现在主容器将接收数据和操作,您可以从那里向子组件发送您需要的任何内容。

The button and the preview image component are stateless component, they just receive props from the parent container.按钮和预览图像组件是无状态组件,它们只是从父容器接收道具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM