-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
组件非列表中,Key(类似wx:key)改变了不能触发组件重新构造(执行生命周期) #4034
Comments
欢迎提交 Issue~ 如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏 如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。 Good luck and happy coding~ |
我啊现在只能自己做个key,每次切换key为true或false {key ? <MyComponent /> : <MyComponent />} |
是小程序么 |
是的啊 |
为啥要根据key来重新渲染呢?你可以更新组件的一个props来触发生命周期的更新 |
在这个问题里,你组件的名为 Key 的 Props 是一个真正的 Props 而不是循环中的 wx:key 对吗? |
是真正的key,不是props |
如果你有一个相对来说复杂的组件,某些props发生变化后,你要手动的去维护initState,然后再手动触发一系列方法。这显然是没有通过key改变触发组件重新构造来的更方便。 |
微信的文档说得模糊不清,我可以理解为 "wx:key 的改变不影响组件渲染,但数据的改变会" 吗... 我也不知道这位朋友理解的正不正确:https://blog.csdn.net/wobaiwodedukuku/article/details/84848948 这里有文章和动图介绍 wx:key 的各种状态:http://www.wxappclub.com/topic/536 |
@Garfield550 这个我晓得,但是这个key仅在列表渲染时有效了。 我的场景是非列表渲染。 Taro如果遵循的是react原则的话能否考虑从框架层面实现一下呢。 |
非列表渲染的 Key 应该只是个普通的 Props 吧,你可以看一下 Taro 编译后的 Key 是什么,wx:key 离开 wx:for 似乎没有什么作用 |
比如你有一个搜索框和搜索结果的时候。 搜索结果这个组件拥有搜索中、搜索失败、加载更多等等状态。 使用key来触发重新触发构造,比手动初始化这些状态,再触发相应的事件要更方便。 <SearchInput onChange={(words)=>{setKeywords(words)}} />
<SearchResult key={keywords} keywords={keywords}/> |
所以,Taro能实现这个小程序里没有实现的功能嘛(滑稽.png) |
因为使用key来更新组件很便捷,react官方也推荐这么做~ |
问题描述
如果将key改为2,能否重新执行一次生命周期呢。(思考ing)
The text was updated successfully, but these errors were encountered: