Skip to content
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

Open
lbb00 opened this issue Aug 1, 2019 · 14 comments

Comments

@lbb00
Copy link

lbb00 commented Aug 1, 2019

问题描述

如果将key改为2,能否重新执行一次生命周期呢。(思考ing)

<MyComponent key={1} />
@taro-bot
Copy link

taro-bot bot commented Aug 1, 2019

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@lbb00
Copy link
Author

lbb00 commented Aug 1, 2019

我啊现在只能自己做个key,每次切换key为true或false

{key ? <MyComponent /> : <MyComponent />}

@luckyadam
Copy link
Member

是小程序么

@lbb00
Copy link
Author

lbb00 commented Aug 1, 2019

是的啊

@shenghanqin
Copy link
Collaborator

为啥要根据key来重新渲染呢?你可以更新组件的一个props来触发生命周期的更新

@Garfield550
Copy link
Collaborator

在这个问题里,你组件的名为 Key 的 Props 是一个真正的 Props 而不是循环中的 wx:key 对吗?

@lbb00
Copy link
Author

lbb00 commented Aug 2, 2019

是真正的key,不是props

@lbb00
Copy link
Author

lbb00 commented Aug 2, 2019

为啥要根据key来重新渲染呢?你可以更新组件的一个props来触发生命周期的更新

如果你有一个相对来说复杂的组件,某些props发生变化后,你要手动的去维护initState,然后再手动触发一系列方法。这显然是没有通过key改变触发组件重新构造来的更方便。

@Garfield550
Copy link
Collaborator

Garfield550 commented Aug 2, 2019

image

微信的文档说得模糊不清,我可以理解为 "wx:key 的改变不影响组件渲染,但数据的改变会" 吗...

我也不知道这位朋友理解的正不正确:https://blog.csdn.net/wobaiwodedukuku/article/details/84848948

这里有文章和动图介绍 wx:key 的各种状态:http://www.wxappclub.com/topic/536

@lbb00 lbb00 changed the title 组件的Key改变了不能触发组件重新渲染(执行生命周期) 组件的Key(类似wx:key)改变了不能触发组件重新渲染(执行生命周期) Aug 2, 2019
@lbb00
Copy link
Author

lbb00 commented Aug 2, 2019

@Garfield550 这个我晓得,但是这个key仅在列表渲染时有效了。

我的场景是非列表渲染。

Taro如果遵循的是react原则的话能否考虑从框架层面实现一下呢。

@Garfield550
Copy link
Collaborator

非列表渲染的 Key 应该只是个普通的 Props 吧,你可以看一下 Taro 编译后的 Key 是什么,wx:key 离开 wx:for 似乎没有什么作用

@lbb00
Copy link
Author

lbb00 commented Aug 2, 2019

比如你有一个搜索框和搜索结果的时候。

搜索结果这个组件拥有搜索中、搜索失败、加载更多等等状态。

使用key来触发重新触发构造,比手动初始化这些状态,再触发相应的事件要更方便。

<SearchInput onChange={(words)=>{setKeywords(words)}} />
<SearchResult key={keywords} keywords={keywords}/>

@lbb00
Copy link
Author

lbb00 commented Aug 2, 2019

非列表渲染的 Key 应该只是个普通的 Props 吧,你可以看一下 Taro 编译后的 Key 是什么,wx:key 离开 wx:for 似乎没有什么作用

所以,Taro能实现这个小程序里没有实现的功能嘛(滑稽.png)

@lbb00 lbb00 changed the title 组件的Key(类似wx:key)改变了不能触发组件重新渲染(执行生命周期) 组件的Key(类似wx:key)改变了不能触发组件重新构造(执行生命周期) Aug 2, 2019
@lbb00 lbb00 changed the title 组件的Key(类似wx:key)改变了不能触发组件重新构造(执行生命周期) 组件非列表中,Key(类似wx:key)改变了不能触发组件重新构造(执行生命周期) Aug 2, 2019
@wakefun
Copy link

wakefun commented May 28, 2020

为啥要根据key来重新渲染呢?你可以更新组件的一个props来触发生命周期的更新

因为使用key来更新组件很便捷,react官方也推荐这么做~
https://zh-hans.reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-uncontrolled-component-with-a-key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants