在react中,柯里化是一种关于函数的高阶技术,指的是通过函数继续返回函数的方式,实现多次接收参数最后统一处理的函数编码形式;柯里化不会调用函数,只是对函数进行转换,通过柯里化在处理表单时,可以轻松的获取表单控件数据。
本教程操作环境:Windows10系统、react17.0.1版、Dell G3电脑。
什么是react柯里化
函数的柯里化:
通过函数调用继续返回函数的方式,实现多次接受参数最后统一处理的函数编码形式.
扩展:
高阶函数: 若一个函数符合下面两个规范中的一个,该函数就是高阶函数
1.若a函数,接受的参数是一个函数,那么a就可以称为高阶函数
2.若a函数,调用的返回值依旧是一个函数,那么a就可以称之为高阶函数
3.常见的高阶函数有:promise,setTimeout,arr.map等
示例如下;
在form表单中,使用受控组件绑定状态数据,实现点击显示表单数据:
import React, {Component} from 'react'; export default class Form extends Component{ state = { userName: '', password: '' } submitForm = (event) => { event.preventDefault() //阻止表单提交 const {userName, password } = this.state; alert(`${userName}, ${password}`) } updateUserName = (event) => { this.setState({ userName: event.target.value, }) } updatePassword = (event) => { this.setState({ password: event.target.value, }) } render() { return ( <form onSubmit={this.submitForm}> 用户名:<input type="text" name="userName" onChange={this.updateUserName}/> 密码: <input type="password" name="password" onChange={this.updatePassword}/> <button>登录</button> </form> ) } }
可以看到,这种方法对于表单项多的情况比较繁琐,可以利用函数柯里化来优化:
import React, {Component} from 'react'; export default class Form extends Component{ state = { userName: '', password: '' } submitForm = (event) => { event.preventDefault() //阻止表单提交 const {userName, password } = this.state; alert(`${userName}, ${password}`) } updateFormData = (key) => { return (event) => { this.setState({ [key]: event.target.value, }) } } render() { return ( <form onSubmit={this.submitForm}> 用户名:<input type="text" name="userName" onChange={this.updateFormData('userName')}/> 密码: <input type="password" name="password" onChange={this.updateFormData('password')}/> <button>登录</button> </form> ) } }
this.updateFormData()的返回值是一个回调函数,绑定为onChange的事件,参数为event。这样就可以在初次调用时传类型,触发改变事件时传值了。
不使用函数柯里化的实现
直接在onChange事件绑定为回调,可以实现同时传递类型和值两个参数。
import React, {Component} from 'react'; export default class Form extends Component{ state = { userName: '', password: '' } submitForm = (event) => { event.preventDefault() //阻止表单提交 const {userName, password } = this.state; alert(`${userName}, ${password}`) } updateFormData = (key, event) => { this.setState({ [key]: event.target.value, }) } render() { return ( <form onSubmit={this.submitForm}> 用户名:<input type="text" name="userName" onChange={(event) => this.updateFormData('userName', event)}/> 密码: <input type="password" name="password" onChange={(event) => this.updateFormData('password', event)}/> <button>登录</button> </form> ) } }
【相关推荐:javascript视频教程、web前端】
以上是什么是react柯里化的详细内容。更多信息请关注PHP中文网其他相关文章!

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

html5aimstoenhancewebcapabilities,Makeitmoredynamic,互动,可及可访问。1)ITSupportsMultimediaElementsLikeAnd,消除innewingtheneedtheneedtheneedforplugins.2)SemanticeLelelemeneLementelementsimproveaCceccessibility inmproveAccessibility andcoderabilitile andcoderability.3)emply.3)lighteppoperable popperappoperable -poseive weepivewebappll

html5aimstoenhancewebdevelopmentanduserexperiencethroughsemantstructure,多媒体综合和performanceimprovements.1)SemanticeLementLike like,和ImproVereAdiability and ImproVereAdabilityAncccossibility.2)和TagsallowsemplowsemplowseamemelesseamlessallowsemlessemlessemelessmultimedimeDiaiiaemediaiaembedwitWithItWitTplulurugIns.3)

html5isnotinerysecure,butitsfeaturescanleadtosecurityrisksifmissusedorimproperlyimplempled.1)usethesand andboxattributeIniframestoconoconoconoContoContoContoContoContoconToconToconToconToconToconTedContDedContentContentPrevulnerabilityLikeClickLickLickLickLickLickjAckJackJacking.2)

HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

使用ID选择器在CSS中并非固有地不好,但应谨慎使用。1)ID选择器适用于唯一元素或JavaScript钩子。2)对于一般样式,应使用类选择器,因为它们更灵活和可维护。通过平衡ID和类的使用,可以实现更robust和efficient的CSS架构。

html5'sgoalsin2024focusonrefinement和optimization,notnewfeatures.1)增强performandemandeffifice throughOptimizedRendering.2)risteccessibilitywithrefinedibilitywithRefineDatientAttributesAndEllements.3)expliencernsandelements.3)explastsecurityConcerns,尤其是withercervion.4)

html5aimedtotoimprovewebdevelopmentInfourKeyAreas:1)多中心供应,2)语义结构,3)formcapabilities.1)offlineandstorageoptions.1)html5intoryements html5introctosements introdements and toctosements and toctosements,简化了inifyingmediaembedingmediabbeddingingandenhangingusexperience.2)newsements.2)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Atom编辑器mac版下载
最流行的的开源编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境