-
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
结合redux使用时,useEffect中使用setNavigationBarTitle,当在下层级页面修改数据,会在下层级页面中修改标题,而不是期望的回到当前页面再改变 #5167
Comments
CC @Chen-jj |
欢迎提交 Issue~ 如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏 如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。 Good luck and happy coding~ |
目前要实现期望特性只能自己用hooks来处理了。 export function useSetPageTitle(title) {
const lastTitleRef = useRef('');
const pageOnShowRef = useRef(false);
const doSetTitle = (title) => {
if (lastTitleRef.current !== title) {
lastTitleRef.current = title;
Taro.setNavigationBarTitle({ title });
}
};
useDidShow(() => {
pageOnShowRef.current = true;
doSetTitle(title);
});
useDidHide(() => {
pageOnShowRef.current = false;
});
useEffect(() => {
if (pageOnShowRef.current) {
doSetTitle(title);
}
}, [title]);
} |
我也碰到过这个问题,我的理解是,因为进入下一页并不会销毁之前的页面,所以之前的页面由于订阅的redux值变了导致重新渲染,执行了相应的代码。我觉得,是不是你在之前那个页面的 useDidshow 里面再设置一遍title就行了 |
然后你这样的写法,如果页面中还需要用到 useDidShow useDidHide 怎么办? 我不知道是不是可以同时有多个 useDidShow 存在同一个页面组件中 |
@dpyzo0o 正解。小程序不像 H5,并没有销毁上一个页面,useEffect 执行是正确的。useDidShow 里处理吧。 |
@dpyzo0o 可以重复。会叠加调用。 taro/packages/taro/src/hooks.js Lines 40 to 63 in 205d62d
|
问题描述
复现步骤
如上
期望行为
A中的useEffect仅在A页面激活时触发,而不是在B页面中调用A页面的useEffect
报错信息
无
系统信息
👽 Taro v1.3.28
Taro CLI 1.3.28 environment info:
System:
OS: macOS 10.14
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 8.9.3 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 5.5.1 - /usr/local/bin/npm
npmPackages:
@tarojs/async-await: ^1.3.28 => 1.3.28
@tarojs/components: ^1.3.28 => 1.3.28
@tarojs/plugin-babel: ^1.3.28 => 1.3.28
@tarojs/plugin-csso: ^1.3.28 => 1.3.28
@tarojs/plugin-less: ^1.3.28 => 1.3.28
@tarojs/plugin-uglifyjs: ^1.3.28 => 1.3.28
@tarojs/redux: ^1.3.28 => 1.3.28
@tarojs/router: ^1.3.28 => 1.3.28
@tarojs/taro: ^1.3.28 => 1.3.28
@tarojs/taro-weapp: ^1.3.28 => 1.3.28
@tarojs/webpack-runner: ^1.3.28 => 1.3.28
eslint-config-taro: ^1.3.28 => 1.3.28
eslint-plugin-taro: ^1.3.28 => 1.3.28
nerv-devtools: ^1.5.0 => 1.5.1
nervjs: ^1.5.0 => 1.5.1
stylelint-config-taro-rn: ^1.3.28 => 1.3.28
npmGlobalPackages:
typescript: 1.8.10
补充信息
The text was updated successfully, but these errors were encountered: