javascript - SwipeableViews 如何将 updateHeight() 作为道具传递
问题描述
所以我使用 SwipeableViews 没有在页面之间滑动。我的问题是每个页面都有不同的动态长度。我将 animateHeight 设置为 true,但它仅在选项卡更改时为高度设置动画。在文档中它说函数 updateHeight() 可以解决这个问题。 https://react-swipeable-views.com/api/api/
由于我缺乏知识,我无法获得 updateHeight,因为我在基于类的应用程序上看到了所有示例。我在基于功能的应用程序中构建了我的应用程序。我只是想不通将它作为道具传递给以后调用它。
解决方案
我找到了一种使用 updateHeight 函数的方法。这有点hacky,但它有效。我添加了一个间隔和超时来补偿数据加载/慢速机器。useEffect 清除页面卸载时的间隔。
可能有更好的解决方案,但这是我迄今为止发现的!
export function SwipeView() {
const [ref, setRef] = useState(null);
const onRefChange = useCallback((node: SwipeableViews & HTMLDivElement) => {
// hacky solution to update the height after the first render. Height is not set correctly on initial render
setRef(node); // e.g. change ref state to trigger re-render
if (node === null) {
return;
} else {
interval = setInterval(() => {
// @ts-ignore typings are not correct in this package
node.updateHeight();
}, 100);
setTimeout(() => {
clearInterval(interval);
}, 10000);
}
}, []);
useEffect(() => {
return () => clearInterval(interval);
}, [interval]);
return (
<SwipeableViews
animateHeight
ref={onRefChange}
>
{children}
</SwipeableViews>
);
}
推荐阅读
- regex - 如何使用 RegEx 收集多个登录页面 URL 的信息?
- material-ui - @material-ui/core 未定义的匹配
- php - PHP 确实改变了循环的范围并在包含的文件中切换
- c - 如何使用格式说明符打印先前定义的运算符 (+-%/)
- javascript - 我如何使嵌入式pdf在移动设备上工作
- java - 从 ArrayList 返回对象错误地涉及自定义异常
- package - 在 linux-mint 18 Sarah 上安装 erlang-solutions 时遇到错误
- typescript - 在界面中为 firebase 函数/storgae/database 编写类型
- html - 复选框功能单击图像而不是复选框
- pip - lxml 无法使用 python 3.7 安装