Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k 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 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...