摘要:背景在的方法中使用來保存的話,會(huì)導(dǎo)致輸入卡頓,原因是用戶在輸入時(shí),一直在,導(dǎo)致整個(gè)頁面一直重新渲染主頁面解決方法將組件多帶帶封裝成一個(gè)組件,這樣就只會(huì)觸發(fā)自身重新渲染而不是整個(gè)頁面組件主頁面發(fā)送內(nèi)容發(fā)送這樣就可以讓用戶愉快地輸入的同時(shí),的值啦
背景:
在的onChange方法中使用setState來保存value的話,會(huì)導(dǎo)致輸入卡頓,原因是用戶在輸入時(shí),一直在setState,導(dǎo)致整個(gè)頁面一直重新渲染
主頁面:
import React, { Component, } from "react"; import { Input } from "antd"; const { TextArea } = Input; class CustomCompent extends Component { constructor(props) { super(props); this.state = { targetValue: "", }; } handleChange = e => { let targetValue = e.target.value; this.setState({ targetValue, }); }; render() { const { targetValue } = this.state; return ( <> xxxx xxxx{targetValue.length}/100
> );}
解決方法:
將組件多帶帶封裝成一個(gè)組件(component),這樣就只會(huì)觸發(fā)自身重新渲染而不是整個(gè)頁面!
TextArea 組件:
import React from "react"; import { Input } from "antd"; const { TextArea } = Input; class CountTextArea extends React.PureComponent { constructor(props) { super(props); this.state = { targetValue: "", }; } handleChange = value => { let targetValue = value.target.value; this.setState({ targetValue, }); }; render() { const { setRef } = this.props; const { targetValue } = this.state; return ( <>{targetValue.length}/100
> ); } } export default CountTextArea;
主頁面:
import React, { Component, } from "react"; import { Button } from "antd"; import CountTextArea from "./CountTextArea"; class CustomCompent extends Component { componentDidMount() { this.customTextArea = React.createRef(); } handleOk = () => { if (this.customTextArea && this.customTextArea.current) { //發(fā)送內(nèi)容 let value =this.customTextArea.current.textAreaRef.value //xxx } } render() { return ( <>> ); } }
這樣就可以讓用戶愉快地輸入的同時(shí),setState textarea 的值啦~
(完)
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/109639.html
摘要:受控輸入框只會(huì)顯示通過傳入的數(shù)據(jù)。例如,數(shù)組中的元素將會(huì)渲染三個(gè)單選框或復(fù)選框。屬性接收一個(gè)布爾值,用來表示組件是否應(yīng)該被渲染成選中狀態(tài)。 原文地址:React.js Forms: Controlled Components 原文作者:Loren Stewart 譯者:小 B0Y 校對(duì)者:珂珂君 本文涵蓋以下受控組件: 文本輸入框 數(shù)字輸入框 單選框 復(fù)選框 文本域 下拉...
摘要:基礎(chǔ)創(chuàng)建虛擬參數(shù)元素名稱,例如參數(shù)屬性集合,例如,,,從參數(shù)開始,表示該元素的子元素,通常這些元素通過創(chuàng)建,文本文件可以直接插入嘻嘻哈哈這是渲染器,將元素渲染到頁面中。 React簡介 FeceBook開源的一套框架,專注于MVC的視圖V模塊。實(shí)質(zhì)是對(duì)V視圖的一種實(shí)現(xiàn)。 React框架的設(shè)計(jì)沒有過分依賴于某個(gè)環(huán)境,它自建一套環(huán)境,就是virtual DOM(虛擬DOM)。 提供基礎(chǔ)AP...
摘要:傳入一個(gè)對(duì)象的時(shí)候,這個(gè)對(duì)象表示該組件的新狀態(tài)。下一節(jié)中我們將介紹小書配置組件的。 React.js 小書 Lesson10 - 組件的 state 和 setState 本文作者:胡子大哈本文原文:http://huziketang.com/books/react/lesson10 轉(zhuǎn)載請注明出處,保留原文鏈接以及作者信息 在線閱讀:http://huziketang.com/bo...
摘要:希望大家在這浮夸的前端圈里,保持冷靜,堅(jiān)持每天花分鐘來學(xué)習(xí)與思考。 今天的React題沒有太多的故事…… 半個(gè)月前出了248個(gè)Vue的知識(shí)點(diǎn),受到很多朋友的關(guān)注,都強(qiáng)烈要求再出多些React相前的面試題,受到大家的邀請,我又找了20多個(gè)React的使用者,他們給出了328道React的面試題,由我整理好發(fā)給大家,同時(shí)發(fā)布在了前端面試每日3+1的React專題,希望對(duì)大家有所幫助,同時(shí)大...
閱讀 1415·2021-11-25 09:43
閱讀 2274·2021-09-27 13:36
閱讀 1128·2021-09-04 16:40
閱讀 1967·2019-08-30 11:12
閱讀 3325·2019-08-29 14:14
閱讀 579·2019-08-28 17:56
閱讀 1338·2019-08-26 13:50
閱讀 1260·2019-08-26 13:29