如何在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.pswp.cn/news/393011.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393011.shtml
英文地址,請注明出處:http://en.pswp.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…