简体   繁体   English

按下子组件按钮时反应 Redux 如果 websocket 客户端在父级中,如何让 websocket 客户端发送数据?

[英]React Redux when the child component button is pressed how to have the websocket client send data if the websocket client is in the parent?

In my parent component I have在我的父组件中,我有

componentWillReceiveProps(nextProps) {
 if(nextProps.message) {
    websocket.send(JSON.stringfy(nextProps.message));
  }
}

My child component has this button when clicked it runs this method:我的子组件在单击时具有此按钮,它运行此方法:

onClickButton = () => {
  // My action dispatcher for reducer
  this.props.sendMessage(this.props.data);
}

My thought process was that to pass something from child to parent component - I will utilize the store.我的思考过程是将某些东西从子组件传递给父组件 - 我将利用商店。 So whenever the button in a child component of parent component (which is a list) is clicked then the data that comes from child component will be accessible to the parent.因此,每当单击父组件(即列表)的子组件中的按钮时,父组件就可以访问来自子组件的数据。 The reason why I did this is because the websocket client is situated at the parent component.我这样做的原因是因为 websocket 客户端位于父组件中。

The problem is that when the button in the child component is clicked once it runs the componentWillReceiveProps(nextProps) method in the parent component;问题是,当子组件中的按钮一旦在父组件中运行 componentWillReceiveProps(nextProps) 方法时被点击;

however, when I click it again it doesn't run.但是,当我再次单击它时,它不会运行。 I understand that the payload doesn't change but gets set to the same thing again, so I thought when the dispatch action is called again after clicking on the button it will at least have the componentWillReceiveProps(nextProps) method in parent run again?我知道有效负载不会改变,但会再次设置为相同的东西,所以我想当点击按钮后再次调用调度操作时,它至少会让父级中的 componentWillReceiveProps(nextProps) 方法再次运行?

Is there a way to have to have it so that, when I click on a button of a child component - the websocket that is in my parent component send the data of the individual child component?有没有办法让它,当我点击一个子组件的按钮时 - 我的父组件中的 websocket 发送单个子组件的数据?

Essentially, I wanted to have the websocket client send messages to the server when the button in the child component is clicked本质上,我想让 websocket 客户端在单击子组件中的按钮时向服务器发送消息

perhaps a different way to think about your question is to simply pass a function sendMessageToServer and data as props to the child component.也许考虑您的问题的另一种方式是简单地将 function sendMessageToServerdata作为道具传递给子组件。 as a functional component, it would look something like this:作为一个功能组件,它看起来像这样:

export default function Parent() {
  // this child component doesnt have to be here, can be imported or declared outside of this Parent functional component. it's here for demo purposes.
  const Child = ({ data, sendMessageToServer }) => {
    return <button onClick={() => sendMessageToServer(data)}>Send Data</button>;
  };
  const websocket = {
    send(msg) {
      console.log(msg);
    }
  };
  const sendMessageToServer = msg => {
    websocket.send(JSON.stringify(msg));
  };
  const data = { msg: "message from parent" };
  return (
    <div className="App">
      <Child {...{ data, sendMessageToServer }} />
    </div>
  );
}

in doing so, the function executes in the parent's scope whenever the button is clicked in the child.这样做时,只要在子级中单击按钮,function 就会在父级的 scope 中执行。 this can of course be written as a class component as well.这当然也可以写成 class 组件。 for illustration purposes, i thought a functional component is easier to understand.出于说明目的,我认为功能组件更容易理解。

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

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