RN的父传子和子传父及方法调用(函数式组件)

news/2024/7/15 17:44:58 标签: react native, react.js, javascript

在React Native中,父组件向子组件传递数据通常通过props实现,而子组件向父组件传递数据则通常通过回调函数实现。下面是一个简单的示例,演示了父组件向子组件传递数据和子组件向父组件传递数据的方法:

父传子

父组件

// ParentComponent.js
import React from 'react';
import { View } from 'react-native';
import ChildComponent from './ChildComponent';

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

  return (
    <View>
      <ChildComponent data={dataFromParent} />
    </View>
  );
}

export default ParentComponent;

子组件

// ChildComponent.js
import React from 'react';
import { Text } from 'react-native';

const ChildComponent = ({ data }) => {
  return (
    <Text>{data}</Text>
  );
}

export default ChildComponent;

在这个例子中,父组件ParentComponent通过props向子组件ChildComponent传递了一个名为data的数据。

子传父

父组件

// ParentComponent.js
import React, { useState } from 'react';
import { View, Text } from 'react-native';
import ChildComponent from './ChildComponent';

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

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

  return (
    <View>
      <Text>Data from Child: {dataFromChild}</Text>
      <ChildComponent onDataReceived={handleDataFromChild} />
    </View>
  );
}

export default ParentComponent;

子组件

// ChildComponent.js
import React from 'react';
import { Button } from 'react-native';

const ChildComponent = ({ onDataReceived }) => {
  const sendDataToParent = () => {
    onDataReceived("Hello from Child");
  };

  return (
    <Button title="Send Data to Parent" onPress={sendDataToParent} />
  );
}

export default ChildComponent;

在这个例子中,子组件ChildComponent通过接收一个名为onDataReceived的prop,当按钮被点击时,调用该函数并传递数据给父组件。父组件在接收到数据后更新自己的状态,并重新渲染以反映数据的变化。

父组件调用子组件方法

子组件

import React, { forwardRef } from 'react';

// 使用 React.forwardRef 包装子组件
const ChildComponent = forwardRef((props, ref) => {
  // 定义子组件的方法
  const childMethod = () => {
    console.log('Child method called');
  };

  // 将子组件的方法绑定到 ref
  React.useImperativeHandle(ref, () => ({
    childMethod,
  }));

  return (
    <div>
      <p>Child Component</p>
    </div>
  );
});

export default ChildComponent;

父组件

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

const ParentComponent = () => {
  // 创建一个 ref 来引用子组件
  const childRef = useRef(null);

  // 在父组件中定义一个方法,用于调用子组件的方法
  const callChildMethod = () => {
    // 检查子组件是否已经挂载
    if (childRef.current) {
      // 调用子组件的方法
      childRef.current.childMethod();
    }
  };

  return (
    <div>
      {/* 将 ref 传递给子组件 */}
      <ChildComponent ref={childRef} />
      {/* 按钮用于触发父组件中的方法 */}
      <button onClick={callChildMethod}>Call Child Method</button>
    </div>
  );
};

export default ParentComponent;

子组件调用父组件的方法

父组件

// 父组件
import React from 'react';
import ChildComponent from './ChildComponent';

const ParentComponent = () => {
  // 定义父组件的方法
  const parentMethod = () => {
    console.log('Parent method called');
  };

  return (
    <div>
      {/* 将父组件的方法作为 prop 传递给子组件 */}
      <ChildComponent parentMethod={parentMethod} />
    </div>
  );
};

export default ParentComponent;

子组件

// 子组件 ChildComponent.js
import React from 'react';

const ChildComponent = ({ parentMethod }) => {
  const handleClick = () => {
    // 在子组件中调用父组件传递的方法
    parentMethod();
  };

  return (
    <div>
      <button onClick={handleClick}>Call Parent Method</button>
    </div>
  );
};

export default ChildComponent;


http://www.niftyadmin.cn/n/5416879.html

相关文章

基于梯度统计学的渐变型亮缝识别算法

作者&#xff1a;翟天保Steven 版权声明&#xff1a;著作权归作者所有&#xff0c;商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处 一、场景痛点 在图像处理相关的实际工程中&#xff0c;会出现各式各样的现实复杂问题&#xff0c;有的是因为机械设计导致&#x…

#QT(网络编程-UDP)

1.IDE&#xff1a;QTCreator 2.实验&#xff1a;UDP 不分客户端和服务端 3.记录 &#xff08;1&#xff09;做一个UI界面 &#xff08;2&#xff09;编写open按钮代码进行测试&#xff08;用网络调试助手测试&#xff09; &#xff08;3&#xff09;完善其他功能测试 4.代码 …

c# 多线程创建及线程同步

在C#中&#xff0c;线程是使用Thread类处理的&#xff0c;该类在System.Threading命名空间中。 ManagedThreadId是确认线程的唯一标识符&#xff0c;程序在大部分情况下都是通过Thread.ManagedThreadId来辨别线程的。如获取当前线程的id&#xff1a; int a Thread.CurrentTh…

SpringBoot整合Redis实现分布式锁

SpringBoot整合Redis实现分布式锁 分布式系统为什么要使用分布式锁&#xff1f; 首先&#xff0c;分布式系统是由多个独立节点组成的&#xff0c;这些节点可能运行在不同的物理或虚拟机器上&#xff0c;它们通过网络进行通信和协作。在这样的环境中&#xff0c;多个节点可能同…

海洋科学—物理海洋学 第八章 大气与海洋

一、地球大气的平均状态 1、地球大气的成分与气象要素 &#xff08;1&#xff09;地球大气的成分 地球大气由多种气体组成&#xff0c;并掺有一些悬浮的固体和液体微粒。在85 km 以下的各种气体成分中&#xff0c; 一般可分为两类。 一类称为定常成分&#xff0c;各成分间大致保…

JavaWeb基础入门——(二)MySQL数据库基础(5-存储过程)

八、存储过程 8.1 存储过程介绍 8.1.1 SQL指令执行过程 从SQL执行执行的流程中我们分析存在的问题&#xff1a; 如果我们需要重复多次执行相同的SQL&#xff0c;SQL指令都需要通过连接传递到MySQL&#xff0c;并且需要经过编译和执行的步骤&#xff1b; 如果我们需要连续执行…

【CCS2022】STAR: Secret Sharing for Private Threshold Aggregation Reporting

STAR为数据收集提供了一种新的、更为私密和有效的方法&#xff0c;能够帮助开发者在收集关键的应用使用信息的同时&#xff0c;保护用户的隐私。 文章目录 1.论文原文2.概述3.主要背景4.使用的密码学原语4.1 对称加密4.2 秘密共享4.3 VOPRF&#xff08;可验证的不经意伪随机数函…

[数据集][目标检测]遥感图像游泳池检测数据集VOC+YOLO格式288张1类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;288 标注数量(xml文件个数)&#xff1a;288 标注数量(txt文件个数)&#xff1a;288 标注类别…