Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

react 的 input onchange如何配合useEffect

<AtInput
  value={holder_info.mobile}
  onChange={val => updateHolder(val, "mobile")}
></AtInput>
<AtInput
  value={holder_info.name}
  onChange={val => updateHolder(val, "name")}
></AtInput>

如上图,即使updateHolder用useCallback包裹了也没用,因为onChange传递的是()=> { return updateHolder },传递到AtInput中每次都会没认为是一个新函数。

这就导致一个重复渲染的问题:某一个AtInput触发onchange,导致父组件更新,其他AtInput组件侦测到传递下来的onChange函数的变化,也触发re-render。这该如何避免呢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

导致组件更新,是因为你在updateHolder中set了state吧,那必然会触发useEffect,不然怎么能render到改变呢,不想让其他组件render的话,就把相关组件用memo包起来吧。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...