-
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
刚升级了3.0.8,发现useEffect和useLayoutEffect的执行时机和预期的不一致,无法在其中获取元素宽高 #7491
Comments
尝试使用jquery like 的方式也不行 $('user-header').height() 这个哥们在useLayoutEffect中使用递归的方式来获取宽高,如果获取失败就持续递归,直到获取成功 |
使用 |
@Chen-jj useReady 只会执行一次啊,我的需求是需要根据用户是否登录,用户信息的标签长度,获取高度,这个view是一个flex布局的 |
useEffect、 useLayoutEffect 都是 React 根据 DOM API 执行情况来触发的,而 Taro 3 DOM 层是在小程序逻辑层模拟的,setData 完成后才能用 selectorQuery 获取小程序 DOM 的尺寸信息。你可以试试在 useEffect 里调用 Taro.nextTick,在 nextTick 继续获取小程序 DOM,这个 nextTick 应该能确保是 setData 回调里执行。
taro/packages/taro-runtime/src/dom/root.ts Lines 88 to 96 in 9e03ecd
|
通过nextTick和setTimeOut(100) 基本解决了这个问题,没有复现过~ |
👍🏻👍🏻👍🏻💪💪💪老哥有空的话欢迎吧问题与解决办法提个 PR 到文档,指引其他开发朋友~ |
为啥要需要setTimeout? |
setTimeout 能解决一些时序问题, 但是不是可靠的. |
对呀!页面级的可以直接 useReady 加 nextTick |
这个解决方案并不适用于在 // set some states
setTimeout(() => {
Taro.nextTick(() => {
// render ready
})
}, 0) |
相关平台
微信小程序
小程序基础库: 2.11.3
使用框架: React
复现步骤
期望结果
res 返回空
实际结果
获取渲染后的组件宽高
环境信息
The text was updated successfully, but these errors were encountered: