如何在React中从其父组件更改子组件的状态

by Johny Thomas

约翰尼·托马斯(Johny Thomas)

如何在React中从其父组件更改子组件的状态 (How to change the state of a child component from its parent in React)

We will be building a simple React app which shows the real name of a superhero on a button click.

我们将构建一个简单的React应用程序,单击按钮即可显示超级英雄的真实姓名。

Let’s get started.

让我们开始吧。

First, we will create a Superhero component with a name attribute in state. This component will render that name first.

首先,我们将创建一个具有name属性处于状态的Superhero组件。 该组件将首先呈现该name

Now let’s create a function changeName() in the Superhero component. This function will change the name in state to the actual name of the superhero.

现在,让我们在Superhero组件中创建一个函数changeName() 。 此功能会将状态下的名称更改为超级英雄的实际名称。

Now we have the Superhero component which shows the superhero name and a function which updates the name to his real name.

现在,我们有了显示超级英雄名称的Superhero组件和将名称更新为真实姓名的函数。

The complete Superhero component will look like this:

完整的Superhero组件如下所示:

Now let’s create the App component which will render this Superhero component and a button. When we click the button it shows the real superhero name.

现在,让我们创建App组件,该组件将呈现此Superhero组件和一个按钮。 当我们单击按钮时,它将显示真实的超级英雄名称。

We have added a function handleClick() which will get called when the user clicks the button. We need to figure out a way to update the state of the child component, that is the Superhero component.

我们添加了一个handleClick()函数,该函数将在用户单击按钮时调用。 我们需要找出一种更新子组件(即Superhero组件)状态的方法。

We have created a function changeName() in the Superhero component. This function will show the real name of the superhero. If we can call this function from the App component, our work is done. So we will call this function.

我们已经在Superhero组件中创建了一个函数changeName() 。 此功能将显示超级英雄的真实姓名。 如果我们可以从App组件中调用此函数,那么我们的工作就完成了。 因此,我们将调用此函数。

Here is where refs come to our rescue.

裁判在这里为我们解救。

Let’s create a ref of the Superhero component in the App component. Here is the code for doing that.

让我们在App组件中创建Superhero组件的引用。 这是执行此操作的代码。

Here we have created a ref using React.createRef() method and attached the ref to the Superhero component using the ref attribute.

在这里,我们使用React.createRef()方法创建了一个引用,并使用ref属性将ref附加到了Superhero组件上。

Now we will be able to refer the Superhero node using this.superheroElement.current. We will also be able to call the changeName() function in the Superhero component using this.superheroElement.current.changeName().

现在,我们将能够使用this.superheroElement.current引用Superhero节点。 我们还可以使用this.superheroElement.current.changeName()Superhero组件中调用changeName()函数。

Let’s update our handleClick() function in our App component to call the changeName() function.

让我们更新App组件中的handleClick()函数,以调用changeName()函数。

Our handleClick() function will look like this.

我们的handleClick()函数将如下所示。

You can check out the complete code in the below sandbox.

您可以在下面的沙箱中查看完整的代码。

CodeSandboxCodeSandbox is an online editor tailored for web applications.codesandbox.io

CodeSandbox CodeSandbox是为Web应用程序量身定制的在线编辑器。 codesandbox.io

Now we have learned how to update the state of a child component from a parent component ?. I hope this was helpful.

现在我们已经学习了如何从父组件更新子组件的状态? 我希望这可以帮到你。

翻译自: https://www.freecodecamp.org/news/react-changing-state-of-child-component-from-parent-8ab547436271/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393011.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue-property-decorator 提供 OO 的风格 Vue Component 方便类型声明

Prop 父子组件之间传值 Install: npm install --save vue-property-decoratorChild: <template><div>{{fullMessage}}</div> </template><script lang"ts">import Vue from vue import {Component, Prop} from vue-property-decorato…

python学习笔记(1)

变量的命名 变量名只能包含字母、数字、下划线&#xff0c;不能以数字打头不要用Python关键字、函数名、保留用于特殊用途的单词作变量名变量名应短且有描述性慎用小写l和大写O字符串 就是一系列字符 在Python中&#xff0c;用引号扩起的都是字符串&#xff0c;引号可以是单引号…

使用这些HTTP标头保护您的Web应用程序

by Alex Nadalin通过亚历克斯纳达林 使用这些HTTP标头保护您的Web应用程序 (Secure your web application with these HTTP headers) This is part 3 of a series on web security: part 2 was “Web Security: an introduction to HTTP”这是有关Web安全的系列文章的第3部分&…

leetcode547. 朋友圈(并查集)

班上有 N 名学生。其中有些人是朋友&#xff0c;有些则不是。他们的友谊具有是传递性。如果已知 A 是 B 的朋友&#xff0c;B 是 C 的朋友&#xff0c;那么我们可以认为 A 也是 C 的朋友。所谓的朋友圈&#xff0c;是指所有朋友的集合。 给定一个 N * N 的矩阵 M&#xff0c;表…

linux ssh Unused,安装openssh-portable时遇到的问题及解决办法

问题1&#xff1a;configure: error: Your OpenSSL headers do not match yourlibrary. Check config.log for details.If you are sure your installation is consistent, you can disable the checkby running “./configure –without-openssl-header-check”.Also see cont…

windows 删除删除不掉的文件

DEL /F /A /Q \\?\%1RD /S /Q \\?\%1 windows下删除删除不掉的文件&#xff1a; 1、打开记事本&#xff0c;把上面的命令复制进去 2、保存&#xff0c;后缀名改为.bat&#xff0c;ok 3、把想要删除的文件托放到这个文件的图标上 转载于:https://www.cnblogs.com/Mike_Chang/p…

云计算技术的跃进睿云智合专业先进水平

对于未来的云计算数据中心&#xff0c;网络虚拟化方案需要适应计算和存储虚拟化的浪潮&#xff0c;快速的实现云计算业务的发放&#xff0c;以及能够满足动态的应用程序工作负载的需求;同时需要帮助管理员更简单的管理物理网络和虚拟网络&#xff0c;实现网络可视化。睿云智合&…

CSS 选择器权重计算规则

CSS 选择器&#xff08;Selector&#xff09;的权重&#xff08;Specificity&#xff09;决定了对于同一元素&#xff0c;到底哪一条 CSS 规则会生效。且仅有当多条 CSS 规则都对同一元素声明了相应样式时&#xff0c;才会涉及到权重计算的问题。 选择器的分类 正式计算选择器权…

本地构建和自动化构建_如何构建最强大,最安全的家庭自动化系统

本地构建和自动化构建by Amir Off由Amir Off 如何构建最强大&#xff0c;最安全的家庭自动化系统 (How to build the most robust and secure home automation system) In this article, I’ll discuss how I built a Smart Home Automation System with Angular and Node.js …

leetcode990. 等式方程的可满足性(并查集)

给定一个由表示变量之间关系的字符串方程组成的数组&#xff0c;每个字符串方程 equations[i] 的长度为 4&#xff0c;并采用两种不同的形式之一&#xff1a;“ab” 或 “a!b”。在这里&#xff0c;a 和 b 是小写字母&#xff08;不一定不同&#xff09;&#xff0c;表示单字母…

random对文件随机重命名

对文件随机重命名&#xff0c;这个用途可广了&#xff0c;大家可以想想 echo off setlocal ENABLEDELAYEDEXPANSION for /r %%a in (*.txt) do ( set c!random! ren %%~dpnsa.txt !c!.txt) pause 本文转自sucre03 51CTO博客&#xff0c;原文链接&#xff1a;http://blog…

AC日记——Periodic RMQ Problem codeforces 803G

G - Periodic RMQ Problem 思路&#xff1a; 题目给一段序列&#xff0c;然后序列复制很多次&#xff1b; 维护序列很多次后的性质&#xff1b; 线段树动态开点&#xff1b; 来&#xff0c;上代码&#xff1a; #include <cstdio> #include <cstring> #include <…

数据之路 - Python爬虫 - 数据存储

一、文件存储 1.文件打开方式 文件打开方式说明r以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式rb以二进制只读方式打开一个文件。文件指针将会放在文件的开头r以读写方式打开一个文件。文件指针将会放在文件的开头rb以二进制读写方式打开一个文件。文件指针…

创建react应用程序_如何使用React创建一个三层应用程序

创建react应用程序Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!“发现功能JavaScript”被BookAuthority评为最佳新功能编程书籍之一 &#xff01; Splitting a Single Page Application into layers has a …

linux update语句,MySQL 多表 update sql语句总结

MySQL 多表 update 有几种不同的写法。假定我们有两张表&#xff0c;一张表为Product表存放产品信息&#xff0c;其中有产品价格列Price&#xff1b;另外一张表是ProductPrice表&#xff0c;我们要将ProductPrice表中的价格字段Price更新为Price表中价格字段的80%。在Mysql中我…

linux延时与定时操作

1、at ---系统延迟任务发起命令 at time >command ---任务指令 >ctrld ---发起任务 at -l ---列出延时任务Id at -r id ---删除改id任务 at -m ---让无输出的命令产生邮件 at -M ---让有输…

windows修改PowerShell(命令提示符)默认中文编码方式

如果以下方法都没有作用的话&#xff0c;可以直接在代码中调用<stdlib.h>中的system("mode con cp select65001")或者是system("chcp 65001")。当然&#xff0c;前提是你用的也是C、C、C#等强类型编程语言。 **************************************…

leetcode面试题 17.07. 婴儿名字(并查集)

每年&#xff0c;政府都会公布一万个最常见的婴儿名字和它们出现的频率&#xff0c;也就是同名婴儿的数量。有些名字有多种拼法&#xff0c;例如&#xff0c;John 和 Jon 本质上是相同的名字&#xff0c;但被当成了两个名字公布出来。给定两个列表&#xff0c;一个是名字及对应…

appium+python+iOS 环境搭建与使用中常见问题的解决方案链接

&#xff08;1&#xff09;WebDriverAgent 安装入门篇&#xff1a;https://www.cnblogs.com/zhanggui/p/9239827.html 重点摘要&#xff1a; 在WDA的Github上也给出了WDA的特性&#xff1a; 1.支持真机 &&模拟器 在模拟器上运行还是比较方便的&#xff0c;在真机上需要…

api工厂接口路径是什么_为什么(几乎)永远不要再使用绝对路径访问API

api工厂接口路径是什么by Vitaly Kondratiev通过维塔利康德拉季耶夫(Vitaly Kondratiev) 为什么(几乎)永远不要再使用绝对路径访问API (Why you should (almost) never use an absolute path to your APIs again) Recent advances in web apps architecture show that a decou…