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.mzph.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…