Master React Component Communication: Effective Strategies for Efficient Data Sharing

        In React, component communication refers to the process of transferring data, information or performing specific operations between different components. React component communication can be achieved in the following ways:        

1. Parent component passes data to child component (Props): Parent component can pass data to child component through props. Child components receive this data through props and use them to display content or perform actions. This is the most common way of component communication in React.

// 父组件
import ChildCom from "./ChildCom"
const ParentCom = () => {
  const msg = "Hello,my son!";
  return (
    <div>
      <p>组件通信(我是父组件)</p>
      <ChildCom msg={msg}></ChildCom>
    </div>
  );
};
export default ParentCom;

// 子组件
const ChildCom = (props) => {
  const { msg } = props;
  return (
    <div style={
   
   {"borderTop" : "1px solid #000"}}>
      <p>组件通信(我是子组件)</p>
      <p>
        "这是父组件传给我的数据:
        {msg}
      </p>
    </div>
  );
};
export default ChildCom;

operation result:

2. The child component passes data to the parent component (callback function): The child component can pass data to the parent component through the callback function. The parent component passes a callback function as props to the child component. The child component calls the callback function at the appropriate time and passes the data that needs to be passed to the parent component as parameters.

// 父组件
import React, { useState } from "react";

const ParentCom = () => {
  const [msg, setMsg] = useState("Hello,my son!");
  const handleChangeMsg = (newMsg) => {
    setMsg(newMsg);
  };
  return (
    <div>
      <div>组件通信(我是父组件)</div>
      <ChildCom msg={msg} changeMsg={handleChangeMsg}></ChildCom>
    </div>
  );
};

export default ParentCom;

// 子组件
import React, { useState } from "react";

const ChildCom = (props) => {
  const [btnStatus,setBtnStatus] = useState(false);
  const { msg, changeMsg } = props;
  const sendMsgToParent = () => {
    setBtnStatus(true)
    changeMsg("I'm sorry I'm not yours");
  };
  return (
    <div style={
   
   {"borderTop" : "1px solid #000"}}>
      <div>组件通信(我是子组件)</div>
      <div>
        {btnStatus ? "这是我想给父组件的数据:" : "这是父组件传给我的数据:"}
        {msg}
      </div>
      <button onClick={sendMsgToParent}>你认错了,我不是你亲生的</button>
    </div>
  );
};

export default ChildCom;

operation result

3. Transferring data between sibling components (shared state management): When sibling components need to share data, this can be achieved through a shared state management library (such as Redux, Mobx). These libraries allow multiple components to share the same state, making it easier and more efficient to pass data between sibling components.

4. Context API: React provides Context API for sharing data in the component tree, avoiding the trouble of passing props layer by layer. By creating a Context object, the parent component can pass data to all descendant components without having to manually pass it through the intermediate components.

5. Use third-party libraries: In addition to the above methods, there are many third-party libraries and patterns that can implement component communication, such as Event Bus, Observer pattern, etc. These methods can be selected based on specific needs and complexity.

        Choosing the appropriate component communication method depends on your application needs and complexity. For simple data passing, props and callback functions are usually sufficient. For more complex state management and data sharing, you can consider using Redux, Context API or other state management libraries.

Guess you like

Origin blog.csdn.net/LaityMm/article/details/132066585