react函数父组件获取类子组件state

news/2024/7/15 18:11:01 标签: react.js, javascript, 前端

父组件

javascript">import {  Button } from 'antd';
import Child from './Child';
function Parent() {
    let childRef = null;
    const getData = () => {
        console.log(childRef.state.msg)
      }
    return (
        <div>
            <h1>我是父组件</h1>
            <Child ref={(r) => (childRef = r)}></Child>
            <Button onClick={getData}>获取</Button>
        </div>
       
    )
}
export default Parent

 父组件中绑定ref和class组件有所不同,通过ref={(r) => (childRef = r)}绑定

子组件 

javascript">import React from "react";

class child extends React.Component {
    constructor(props) {
        super(props);
        this.state = { msg: '我是子组件' };
    }
    render() {
        return (
            <h1>子组件</h1>
        )
    }
}
export default child;

 最终效果,点击按钮获取子组件的state

 


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

相关文章

Ansible-Playbook介绍

Ansible-Playbook介绍 文章目录Ansible-Playbook介绍1. playbook语法2. 执行playbook3. 执行任务4. 期望状态5. 运行playbooks6. 处理程序7. 管理正在运行的handlers8. handlers变量9. Ansible-pull10. 验证playbookAnsible Playbooks 提供了一种可重复、可重用、简单的配置管理…

YOLOv7 pytorch,支持剪枝【附代码】

yolov7主干部分结构图&#xff1a;yolov7主干 yolov7数据集处理代码&#xff1a;yolov7数据集处理代码 yolov7训练参数解释&#xff1a;yolov7训练参数【与本文代码有区别】 yolov7训练代码详解&#xff1a;yolov7训练代码详解 目录 训练自己的训练集 生成推理阶段的模型…

BoT-SORT: Robust Associations Multi-Pedestrian Tracking 论文详细解读

BoT-SORT: Robust Associations Multi-Pedestrian Tracking 论文详细解读 文章目录BoT-SORT: Robust Associations Multi-Pedestrian Tracking 论文详细解读BoT-SORT:BoT-SORT简述修改卡尔曼滤波状态向量和其他矩阵参数相机的运动补偿IOU与Re-ID的融合实验效果MOT17&#xff1a…

论文阅读:chain of thought Prompting elicits reasoning in large language models

论文阅读&#xff1a;chain of thought Prompting elicits reasoning in large language models 跟着沐神读论文 视频链接&#xff1a;https://www.bilibili.com/video/BV1t8411e7Ug/?spm_id_from333.788&vd_source350cece3ec9a0c2aee50da8ccc315bf4 title:chain of tho…

pwn入门HTB_You know 0xDiablos例题讲解

我希望能将我的疑惑记录&#xff0c;但是堆栈函数调用这些这些&#xff0c;几句话我很难讲清楚&#xff0c;多看教程&#xff0c;好教程很多 至于解题基础&#xff0c;知道栈这种数据结构是一个线性表之后就够了&#xff0c;这题看不懂你来砍我 文章目录前言这题干什么举个例子…

vue面试题(day05)

vue面试题vue3中Composition API 的优势&#xff1f;1.了解 Options ApiCompositioncomposition APi&#xff1a;2.shallowReactive和shallowRef的区别&#xff1f;3.provide与inject如何使用&#xff1f;总结4.toRaw 与 markRaw是什么作用&#xff1f;5.readonly 与 shallowRe…

[ROC-RK3568-PC] [Firefly-Android] 10min带你了解LCD的使用

&#x1f347; 博主主页&#xff1a; 【Systemcall小酒屋】&#x1f347; 博主追寻&#xff1a;热衷于用简单的案例讲述复杂的技术&#xff0c;“假传万卷书&#xff0c;真传一案例”&#xff0c;这是林群院士说过的一句话&#xff0c;另外“成就是最好的老师”&#xff0c;技术…

API的常识与对接,商品详情数据案例分析

原则上API接口设计一般出现在开发的详细设计中&#xff0c;但是随着诸多公司建立开放平台&#xff0c;产品经理也逐渐需要能理解API接口&#xff0c;尤其是做平台性的产品&#xff0c;还要学会定义接口。本文就关于产品经理在设计接口中需要定义什么、需要注意什么来展开陈述。…