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 通过Context将state传递下去了,子组件可以更新该state么?有什么比较好的方式?

子组件可以很方便的是用传递过来的值,但是想要在组件键更新该值。原先是传递一个{}但是这样虽然可以修改,但是没有进过setState不会出发重洗渲染。


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

1 Answer

0 votes
by (71.8m points)

不要在子组件中直接修改父组件的 state。应该通过回调调用父组件的 setState 实现。

官方文档-提升状态


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

...