onclick判斷組件調用_從子組件Onclick更新狀態

onclick判斷組件調用

How to update the state of a parent component from a child component is one of the most commonly asked React questions.

如何從子組件更新父組件的狀態是最常見的React問題之一。

Imagine you're trying to write a simple recipe box application, and this is your code so far:

想象一下,您正在嘗試編寫一個簡單的配方盒應用程序,到目前為止,這是您的代碼:

Eventually you want handleChange to capture what the user enters and update specific recipes.

最終,您希望handleChange捕獲用戶輸入的內容并更新特定配方。

But when you try to run your app, you see a lot of errors in the terminal and dev console. Let's take a closer look at what's going on.

但是,當您嘗試運行您的應用程序時,您會在終端和開發控制臺中看到很多錯誤。 讓我們仔細看看發生了什么。

修正錯誤 (Fixing errors)

First in handleSubmit, setState should be this.setState:

首先在handleSubmitsetState應該是this.setState

handleSubmit() {const newRecipe = this.state.recipelist[0].recipe;this.setState({recipeList[0].recipe: newRecipe});
}

You're already prop drilling, or passing things from the parent RecipeBox component to its child EditList properly. But when someone enters text into the input box and clicks "Submit", the state isn't updated the way you expect.

您已經RecipeBox鉆探,或將其從父RecipeBox組件正確傳遞到其子EditList 。 但是,當有人在輸入框中輸入文本并單擊“提交”時,狀態不會按照您期望的方式更新。

如何從子組件更新狀態 (How to update the state from a child component)

Here you're running into issues because you're trying to update the state of a nested array (recipeList[0].recipe: newRecipe). That won't work in React.

在這里您會遇到問題,因為您正在嘗試更新嵌套數組的狀態( recipeList[0].recipe: newRecipe )。 那在React中行不通。

Instead, you need to create a copy of the full recipeList array, modify the value of recipe for the element you want to update in the copied array, and assign the modified array back to this.state.recipeList.

相反,您需要創建完整recipeList數組的副本,為要在復制的數組中更新的元素修改recipe的值,然后將修改后的數組分配回this.state.recipeList

First, use the spread syntax to create a copy of this.state.recipeList:

首先,使用傳播語法創建this.state.recipeList的副本:

handleSubmit() {const recipeList = [...this.state.recipeList];this.setState({recipeList[0].recipe: newRecipe});
}

Then update the recipe for the element you want to update. Let's do the first element as a proof of concept:

然后更新要更新的元素的配方。 讓我們做第一個元素作為概念證明:

handleSubmit() {const recipeList = [...this.state.recipeList];recipeList[0].recipe = this.state.input;this.setState({recipeList[0].recipe: newRecipe});
}

Finally, update the current recipeList with your new copy. Also, set input to an empty string so the textbox is empty after users click "Submit":

最后,用您的新副本更新當前的recipeList 。 另外,將input設置為空字符串,以便用戶單擊“提交”后文本框為空:

handleSubmit() {const recipeList = [...this.state.recipeList];recipeList[0].recipe = this.state.input;this.setState({recipeList,input: ""});
}

翻譯自: https://www.freecodecamp.org/news/updating-state-from-child-component-onclick/

onclick判斷組件調用

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390180.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390180.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390180.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python 列表List的定義及操作

# 列表概念:有序的可變的元素集合# 定義 # 直接定義 nums [1,2,3,4,5]# 通過range函數構造,python2 和python3 版本之間的差異; # python3 用的時候才會去構造 nums range(1,101)# 列表嵌套 # 注意和C語言中數組的區別,是否可…

遞歸分解因數

題目總時間限制: 1000ms 內存限制: 65536kB描述給出一個正整數a&#xff0c;要求分解成若干個正整數的乘積&#xff0c;即a a1 * a2 * a3 * ... * an&#xff0c;并且1 < a1 < a2 < a3 < ... < an&#xff0c;問這樣的分解的種數有多少。注意到a a也是一種分解…

劍指 Offer 51. 數組中的逆序對

劍指 Offer 51. 數組中的逆序對 在數組中的兩個數字&#xff0c;如果前面一個數字大于后面的數字&#xff0c;則這兩個數字組成一個逆序對。輸入一個數組&#xff0c;求出這個數組中的逆序對的總數。 示例 1: 輸入: [7,5,6,4] 輸出: 5 限制&#xff1a; 0 < 數組長度 &…

react 圖像識別_無法在React中基于URL查找圖像

react 圖像識別If youre new to React and are having trouble accessing images stored locally, youre not alone.如果您不熟悉React&#xff0c;并且無法訪問本地存儲的圖像&#xff0c;那么您并不孤單。 Imagine you have your images stored in a directory next to a co…

html單行元素居中顯示,多行元素居左顯示

有很多的業務需要元素或者文字如果單行&#xff0c;居中顯示&#xff0c;如果數據增多&#xff0c;居中顯示代碼&#xff08;直接復制到編輯器可用&#xff09;&#xff1a;<!DOCTYPE html> <html lang"en"> <head> <meta charset"UTF-8&q…

ML.NET 0.2版增加了集群和新示例

在今年的Build大會上&#xff0c;微軟首次發布了ML.NET。ML.NET是開源的、跨平臺的以及運行在.NET上的機器學習框架。微軟的Ankit Asthana宣布該項目已經完成了第二版的開發。第二版增加了幾個新功能&#xff0c;包括名為集群的新機器學習任務&#xff0c;交叉驗證和訓練-測試&…

如何變得井井有條-來之不易的秘訣來組織您的生活

Because of the changes brought about by COVID-19, many people have had to find healthy and productive ways of working remotely. 由于COVID-19帶來的變化&#xff0c;許多人不得不尋找健康有效的遠程工作方式。 Some have been sent home and can continue doing thei…

被未知進程占用端口的解決辦法

echo off echo 這是用來結束一個未知進程占用端口的批處理可執行文件ipconfig /allnetstat -anoecho 請查看以上信息&#xff0c;輸入被占用的端口號:set /p port請輸入port:tasklist|findstr %port%echo 請結合上述程序進行輸入&#xff0c;請**謹慎輸入**set /p program請輸入…

怎樣在減少數據中心成本的同時不犧牲性能?

2019獨角獸企業重金招聘Python工程師標準>>> 導讀雖然組織對數據中心提出了更高的要求&#xff0c;但IT管理人員確實有辦法在嚴格的預算內展開工作。如今&#xff0c;組織認為即使性能預期不斷提高&#xff0c;其數據中心預算也在縮減。盡管2018年IT支出總體預計增長…

賽普拉斯 12864_如何使用賽普拉斯自動化輔助功能測試

賽普拉斯 12864In my previous post, I covered how to add screenshot testing in Cypress to ensure components dont unintentionally change over time. 在上一篇文章中 &#xff0c;我介紹了如何在賽普拉斯中添加屏幕截圖測試&#xff0c;以確保組件不會隨時間變化。 Now…

anaconda在win下和在mac下的安裝區別

1. 在win下安裝anaconda后會提示你選擇環境變量&#xff0c;但是建議使用默認。 于是CMD進入終端和使用navigator進入終端不一樣&#xff0c;前者會提示無此命令&#xff0c;只能通過navigator進入終端 即使在系統變量變量Path里添加了路徑&#xff0c;使用CMD還是不能使用pyth…

fcn從頭開始_如何使用Go從頭開始構建區塊鏈

fcn從頭開始介紹 (Introduction) With Web 3.0 and blockchain becoming more mainstream every day, do you know what blockchain is? Do you know its technical advantages and use-cases?隨著Web 3.0和區塊鏈每天變得越來越主流&#xff0c;您知道什么是區塊鏈嗎&#x…

java實現無序數組結構

一、數組的2種定義方式 數據類型 [] 數組名稱 new 數據類型[數組長度]; 這里 [] 可以放在數組名稱的前面&#xff0c;也可以放在數組名稱的后面&#xff0c;一般放在名稱的前面 數據類型 [] 數組名稱 {數組元素1&#xff0c;數組元素2&#xff0c;......} 這種方式聲明數組的…

Android App 的主角:Activity

Android App 程序主要由4種類型組成&#xff1a; 1.Activity&#xff08;活動&#xff09;&#xff1a;主要負責屏幕顯示畫面&#xff0c;并處理與用戶的互動。每個Android App至少都會有一個Activity&#xff0c;在程序一啟動時顯示主畫面供用戶操作。 2.Service&#xff08;后…

通過構建Paint App學習React Hooks

According to people in the know, React Hooks are hot, hot, hot. In this article, we follow Christian Jensens 14-part tutorial to find out about the basics of this new feature of React. Follow along to find out more! 據知情人士稱&#xff0c;React Hooks很熱&…

正則表達式 匹配常用手機號 (13、15\17\18開頭的十一位手機號)

原文:正則表達式 匹配常用手機號 &#xff08;13、15\17\18開頭的十一位手機號&#xff09;^1[3578]\d{9}$ ^1表示以1開頭&#xff0c;[3578]表示第二位的數字為3578中的任意一個&#xff0c;\d{9}表示0~9范圍內的數字匹配九次,$表示結束&#xff0c;12位以上的數字不匹配。

Npoi導出excel整理(附源碼)

前些日子做了一個簡單的winform程序&#xff0c;需要導出的功能&#xff0c;剛開始省事直接使用微軟的組件&#xff0c;但是導出之后發現效率極其低下&#xff0c;絕對像web那樣使用npoi組件&#xff0c;因此簡單的進行了整理&#xff0c;包括直接根據DataTable導出excel及Data…

44. 通配符匹配

44. 通配符匹配 給定一個字符串 (s) 和一個字符模式 &#xff0c;實現一個支持 ‘?’ 和 ‘*’ 的通配符匹配。 ? 可以匹配任何單個字符。 * 可以匹配任意字符串&#xff08;包括空字符串&#xff09;。 兩個字符串完全匹配才算匹配成功。說明: s 可能為空&#xff0c;且…

遞歸javascript_使用freeCodeCamp挑戰解釋了JavaScript中的遞歸

遞歸javascriptIn this article I will touch on a few important ideas to help you understand Recursion in JavaScript. I’m not going to give a full definition here, but you can take a look at what Wikipedia has to say. 在本文中&#xff0c;我將介紹一些重要的想…

入庫成本與目標成本對比報表中我學到的東西

1、SQL方面&#xff1a; &#xff08;1&#xff09;、用DECODE函數解決除數為零的情況 具體語法&#xff1a; DECODE&#xff08;參數&#xff0c;0&#xff0c;1&#xff0c;參數&#xff09; ->DECODE(TAB1.A8&#xff0c;0&#xff0c;1&#xff0c;TAB1.A8) &#xff08…