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

使用ref,导致useDidShow多次调用 #5050

Closed
K-walker opened this issue Dec 10, 2019 · 4 comments
Closed

使用ref,导致useDidShow多次调用 #5050

K-walker opened this issue Dec 10, 2019 · 4 comments
Assignees

Comments

@K-walker
Copy link

问题描述

在小程序中使用 ref ,在切换底部 Tab 时,比如,当页面A,切换到页面B,再切换回A页面,此时A页面的 useDidShow 会调用2次

复现步骤

  1. 使用taro创建项目
  2. 配置2个tab,用于页面切换
  3. 在A页面中使用 ref,具体代码如下
  4. 来回切换tab页,即可看到useDidShow多次打印
export default function Index () {

	const [list] = useState([1,2,3,4])
	const refs = useRef<any[]>([]);

	useDidShow(() => {
		console.log('useDidShow')
	})
	useDidHide(() => {
		console.log('useDidHide')
	})
	useEffect(() => {
		console.log('useEffect')
	})

	return (
		<View className={style.index}>
			<View className={style.fixedBar}></View>
			<View className={style.scroll}>
				{list.map( (item,index) => {
					return <View key={index+1} ref={ node => refs[index] = node} className={style.item}>{item}</View>
				})}
			</View>
		</View>
	)
}

期望行为

切换tab的时候,useDidShow 只调用一次

报错信息

系统信息

Taro CLI 1.3.23 environment info:
System:
OS: macOS 10.14.4
Shell: 5.3 - /bin/zsh
Binaries:
Node: 10.15.3 - /usr/local/bin/node
Yarn: 1.15.2 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm

@taro-bot
Copy link

taro-bot bot commented Dec 10, 2019

CC @Chen-jj

@taro-bot
Copy link

taro-bot bot commented Dec 10, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@K-walker
Copy link
Author

@Chen-jj
测试发现

<View ref={ node => refs[index] = node} className={style.item}>{item}</View>

ref如果这样循环去赋值,useDidShow就会调用2次,同时useDidHide也是一样

@K-walker
Copy link
Author

如果在当前页面使用 usePullDownRefresh 钩子,切换页面之后,下拉刷新,这个钩子也会被执行2次

Chen-jj added a commit that referenced this issue Dec 17, 2019
计算循环 ref 跑 createData 前,需要先把 Current.index 置空
@Chen-jj Chen-jj closed this as completed Dec 17, 2019
luckyadam pushed a commit that referenced this issue Dec 19, 2019
计算循环 ref 跑 createData 前,需要先把 Current.index 置空
ZakaryCode pushed a commit that referenced this issue Dec 20, 2019
计算循环 ref 跑 createData 前,需要先把 Current.index 置空
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

2 participants