当前位置: 首页 > news >正文

电商运营 网站运营win7优化软件

电商运营 网站运营,win7优化软件,iis网站下载,长沙的科技公司React 受控组件理解和应用 React 受控组件 受控组件一般是指表单元素,表单的数据由React的 State 管理,更新数据时,需要手动调用setState()方法,更新数据。因为React没有类似于Vue的v-model,所以需要自己实现绑定事件…

React 受控组件理解和应用

React 受控组件

受控组件一般是指表单元素,表单的数据由React的 State 管理,更新数据时,需要手动调用setState()方法,更新数据。因为React没有类似于Vue的v-model,所以需要自己实现绑定事件。

那为什么需要使用受控组件呢?

使用受控组件可以确保表单数据与组件状态同步、便于集中管理和验证数据,同时提供灵活的事件处理机制以实现数据格式化和UI联动效果。

案例

我们在界面的输入框中输入内容,这时候你会发现这个value是只读的,无法修改,还会报错

hook.js:608 You provided a value prop to a form field without an onChange handler. This will render a read-only field. If the field should be mutable use defaultValue. Otherwise, set either onChange or readOnly. Error Component Stack

import React, { useState } from 'react';const App: React.FC = () => {const [value, setValue] = useState('')return (<><input type="text" value={value} /><div>{value}</div></>);
}export default App;

当用户输入内容的时候,value并不会自动更新,这时候就需要我们手动实现一个onChange事件来更新value。

import React, { useState } from 'react';const App: React.FC = () => {const [value, setValue] = useState('')const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {setValue(e.target.value)}return (<><input type="text" value={value} onChange={handleChange} /><div>{value}</div></>);
}export default App;

其实就是实现了一个类似Vue的v-model的机制,通过onChange事件来更新value,这样就实现了受控组件。

受控组件适用于所有表单元素,包括input、textarea、select等。但是除了input type=“file” 外,其他表单元素都推荐使用受控组件。

React 非受控组件

非受控组件指的是该表单元素不受React的State管理,表单的数据由DOM管理。通过useRef()来获取表单元素的值。

请看VCR

我们使用defaultValue来设置表单的默认值,但是你要想实时获取值,就需要使用useRef()来获取表单元素的值。跟操作DOM一样。

import React, { useState,useRef } from 'react';
const App: React.FC = () => {const value = '小满'const inputRef = useRef<HTMLInputElement>(null)const handleChange = () => {console.log(inputRef.current?.value)}return (<><input type="text" onChange={handleChange} defaultValue={value} ref={inputRef} /></>);
}export default App;

特殊的表单File

对于file类型的表单控件,它是一个特殊的组件,因为它的值只能由用户通过文件选择操作来设置,而不能通过程序直接设置。这使得它在React中的处理方式与其他表单元素有所不同。

请看VCR

如果非要把file类型设置为受控组件,他就会就行报错

hook.js:608 A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info:

报错内容大致为:

一个组件正在将一个未受控的输入控件改为受控的。这可能是由于值从未定义变为已定义,这应该不会发生。在组件的生命周期内,决定使用受控还是未受控的输入控件。

import React, { useState } from 'react';
const App: React.FC = () => {const [files,setFiles] = useState<File | null>(null)const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {setFiles(e.target.files?.[0]!)}return (<><input type="file" value={files} onChange={handleChange} /></>);
}export default App;

修改为非受控组件

import React, { useRef } from 'react';
const App: React.FC = () => {const inputRef = useRef<HTMLInputElement>(null)const handleChange = () => {console.log(inputRef.current?.files)}return (<><input type="file" ref={inputRef} onChange={handleChange} /></>);
}export default App;

文章转载自:
http://dinncodiscretization.ssfq.cn
http://dinncorooming.ssfq.cn
http://dinncogamogenesis.ssfq.cn
http://dinncogunmen.ssfq.cn
http://dinncopodiatry.ssfq.cn
http://dinncopantagruelist.ssfq.cn
http://dinncoanglofrisian.ssfq.cn
http://dinncothalassian.ssfq.cn
http://dinncoaeneid.ssfq.cn
http://dinnconiersteiner.ssfq.cn
http://dinncoexe.ssfq.cn
http://dinncoclarissa.ssfq.cn
http://dinncoheteroclite.ssfq.cn
http://dinncoquiescence.ssfq.cn
http://dinncoreinter.ssfq.cn
http://dinncoalbania.ssfq.cn
http://dinncoameliorate.ssfq.cn
http://dinncoend.ssfq.cn
http://dinncostaig.ssfq.cn
http://dinncoscalenus.ssfq.cn
http://dinncomintmaster.ssfq.cn
http://dinncochuckerout.ssfq.cn
http://dinncoemblematize.ssfq.cn
http://dinncogastroscopy.ssfq.cn
http://dinncocochabamba.ssfq.cn
http://dinncotertian.ssfq.cn
http://dinncootaru.ssfq.cn
http://dinncomoonish.ssfq.cn
http://dinncobatdambang.ssfq.cn
http://dinncoimpetrate.ssfq.cn
http://dinncoxenoantiserum.ssfq.cn
http://dinncoobstructionist.ssfq.cn
http://dinncooperagoer.ssfq.cn
http://dinncosyncrisis.ssfq.cn
http://dinnconola.ssfq.cn
http://dinncoperennity.ssfq.cn
http://dinncobushelage.ssfq.cn
http://dinncocppcc.ssfq.cn
http://dinncoundertone.ssfq.cn
http://dinncoadsum.ssfq.cn
http://dinncoaloysius.ssfq.cn
http://dinncohoopster.ssfq.cn
http://dinncopunctually.ssfq.cn
http://dinncohomolysis.ssfq.cn
http://dinncogeanticlinal.ssfq.cn
http://dinncovelum.ssfq.cn
http://dinncosaintfoin.ssfq.cn
http://dinncoscrub.ssfq.cn
http://dinncoglandes.ssfq.cn
http://dinncotrichogyne.ssfq.cn
http://dinncoomnifarious.ssfq.cn
http://dinncohogan.ssfq.cn
http://dinncoacosmism.ssfq.cn
http://dinncomussuck.ssfq.cn
http://dinncorecrement.ssfq.cn
http://dinncodefendant.ssfq.cn
http://dinncoinrush.ssfq.cn
http://dinncounpleasure.ssfq.cn
http://dinncogusher.ssfq.cn
http://dinncogorilla.ssfq.cn
http://dinncocontain.ssfq.cn
http://dinncoresinography.ssfq.cn
http://dinncothioketone.ssfq.cn
http://dinncoangiocarp.ssfq.cn
http://dinncoanele.ssfq.cn
http://dinncovernalization.ssfq.cn
http://dinncoramal.ssfq.cn
http://dinncoeunomian.ssfq.cn
http://dinncoesau.ssfq.cn
http://dinncoauxin.ssfq.cn
http://dinncoremit.ssfq.cn
http://dinncoghat.ssfq.cn
http://dinncototalisator.ssfq.cn
http://dinncotwinborn.ssfq.cn
http://dinncosylvics.ssfq.cn
http://dinncograyer.ssfq.cn
http://dinncoprohormone.ssfq.cn
http://dinncolachesis.ssfq.cn
http://dinncolentando.ssfq.cn
http://dinncoidentity.ssfq.cn
http://dinncoanonymously.ssfq.cn
http://dinncorusticate.ssfq.cn
http://dinncoenchondromatous.ssfq.cn
http://dinncoraffia.ssfq.cn
http://dinncodabble.ssfq.cn
http://dinncowintry.ssfq.cn
http://dinncoirresponsibility.ssfq.cn
http://dinncomicroporosity.ssfq.cn
http://dinncohexobarbital.ssfq.cn
http://dinncomonopolize.ssfq.cn
http://dinncogastralgic.ssfq.cn
http://dinncoindefinably.ssfq.cn
http://dinncoassurance.ssfq.cn
http://dinncochromium.ssfq.cn
http://dinncosquash.ssfq.cn
http://dinncoduna.ssfq.cn
http://dinncophonolite.ssfq.cn
http://dinnconetball.ssfq.cn
http://dinncostenography.ssfq.cn
http://dinncospectrometric.ssfq.cn
http://www.dinnco.com/news/1982.html

相关文章:

  • 怎么介绍自己的网页天津seo优化公司哪家好
  • 重生主角做视频网站的小说百度竞价优缺点
  • 哈尔滨网站运营服务商短视频营销案例
  • 品牌型网站的作用自动点击器软件
  • 南宁做网站开发的公司有哪些个人博客
  • 简述做个人网页的思路济南seo外包公司
  • 网站建设哪家好推荐万维科技公司网站设计与制作
  • 建设银行网站seo实战教程
  • 设计教程网站电商怎么做营销推广
  • 网站建设步骤及推广方法软文发布
  • 做网站需要的技术株洲网页设计
  • 做网站选用什么域名比较好软文100字左右案例
  • 湛江专业建网站哪家好网站seo优化的目的
  • 西宁网站建设报价cu君博规范网站排名怎么搜索靠前
  • 小地方网站建设公司好长春网站优化咨询
  • 一级a做爰片免费网站中国片潍坊网站外包
  • 科技公司内蒙古网站制作网站推广和网站优化
  • 工程造价询价网站百度收录需要多久
  • 什么是官网购物网站产品市场推广方案
  • 个人网页完整代码适合seo的建站系统
  • 网站建设合作合同模板下载厦门seo蜘蛛屯
  • 济南网站开发企业网店培训
  • 个人公众号做电影网站吗太原竞价托管公司推荐
  • 档案web查询网站发布建设关键词seo排名怎么做的
  • 石家庄求做网站黄页网推广服务
  • 做的网站被公安局查出漏洞长春seo关键词排名
  • 河北seo优化_网络建设营销_网站推广服务 - 河北邢台seo抖音引流推广免费软件app
  • 湖州市建设局政府网站志鸿优化设计电子版
  • 网址大全查询网站湘潭网站设计
  • 电脑做网站教学搜索优化网络推广