本文介绍: 在React中,父组件向子组件传递数据通过props实现的,而子组件向父组件传递数据需要通过回调函数方式。对于爷孙组件之间通信可以通过在中间组件上设置props回调函数传递数据。兄弟组件之间通信需要通过共享状态或者通过父组件来进行中转。

在React中,父组件向子组件传递数据通过props实现的,而子组件向父组件传递数据需要通过回调函数的方式。对于爷孙组件之间通信可以通过在中间组件上设置props回调函数传递数据。兄弟组件之间通信需要通过共享状态或者通过父组件来进行中转。

以下是一些基本示例代码

1. 父传子:

父组件:

import React from 'react';
import ChildComponent from './ChildComponent';

const ParentComponent = () => {
  const dataFromParent = "Hello from parent";

  return (
    <div>
      <ChildComponent dataFromParent={dataFromParent} />
    </div&gt;
  );
}

export default ParentComponent;

子组件:

import React from 'react';

const ChildComponent = (props) =&gt; {
  return (
    <div&gt;
      <p&gt;{props.dataFromParent}</p&gt;
    </div&gt;
  );
}

export default ChildComponent;

2. 子传父:

父组件:

import React, { useState } from 'react';
import ChildComponent from './ChildComponent';

const ParentComponent = () => {
  const [dataFromChild, setDataFromChild] = useState("");

  const handleDataFromChild = (data) => {
    setDataFromChild(data);
  }

  return (
    <div>
      <p>Data from child: {dataFromChild}</p>
      <ChildComponent onDataFromChild={handleDataFromChild} />
    </div>
  );
}

export default ParentComponent;

子组件:

import React from 'react';

const ChildComponent = (props) => {
  const sendDataToParent = () => {
    const data = "Hello from child";
    props.onDataFromChild(data);
  }

  return (
    <div>
      <button onClick={sendDataToParent}>Send Data to Parent</button>
    </div>
  );
}

export default ChildComponent;

3. 爷孙组件传值

爷爷组件:

import React, { useState } from 'react';
import ParentComponent from './ParentComponent';

const GrandparentComponent = () => {
  const [dataFromGrandchild, setDataFromGrandchild] = useState("");

  const handleDataFromGrandchild = (data) => {
    setDataFromGrandchild(data);
  }

  return (
    <div>
      <p>Data from grandchild: {dataFromGrandchild}</p>
      <ParentComponent onDataFromGrandchild={handleDataFromGrandchild} />
    </div>
  );
}

export default GrandparentComponent;

父组件:

import React from 'react';
import ChildComponent from './ChildComponent';

const ParentComponent = (props) => {
  return (
    <div>
      <ChildComponent onDataFromGrandchild={props.onDataFromGrandchild} />
    </div>
  );
}

export default ParentComponent;

子组件:

import React from 'react';

const ChildComponent = (props) => {
  const sendDataToGrandparent = () => {
    const data = "Hello from grandchild";
    props.onDataFromGrandchild(data);
  }

  return (
    <div>
      <button onClick={sendDataToGrandparent}>Send Data to Grandparent</button>
    </div>
  );
}

export default ChildComponent;

4. 兄弟组件传值

在React中,兄弟组件之间通信通常需要通过共享状态,可以将状态提升到它们的共同的父组件中,然后通过props传递给兄弟组件。

父组件:

import React, { useState } from 'react';
import BrotherComponentA from './BrotherComponentA';
import BrotherComponentB from './BrotherComponentB';

const ParentComponent = () => {
  const [sharedData, setSharedData] = useState("");

  const handleDataChange = (data) => {
    setSharedData(data);
  }

  return (
    <div>
      <BrotherComponentA onSharedDataChange={handleDataChange} />
      <BrotherComponentB sharedData={sharedData} />
    </div>
  );
}

export default ParentComponent;

兄弟组件A:

import React from 'react';

const BrotherComponentA = (props) => {
  const sendDataToBrotherB = () => {
    const data = "Hello from Brother A";
    props.onSharedDataChange(data);
  }

  return (
    <div>
      <button onClick={sendDataToBrotherB}>Send Data to Brother B</button>
    </div>
  );
}

export default BrotherComponentA;

兄弟组件B:

import React from 'react';

const BrotherComponentB = (props) => {
  return (
    <div>
      <p>Data from Brother A: {props.sharedData}</p>
    </div>
  );
}

export default BrotherComponentB;

这些是基本示例,实际应用中根据具体情况可能需要复杂状态管理工具,如Redux或者Context API。

原文地址:https://blog.csdn.net/m0_47999208/article/details/134778096

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_42342.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注