简体   繁体   English

具有动画和useEffect的React.useState

[英]React.useState with animation and useEffect

I am trying to implement animation using a wrapper component using useState and useEffect. 我正在尝试使用useState和useEffect的包装器组件来实现动画。

If a certain value on the props change I would like that to trigger the start of the animation: 如果道具上的某个值发生变化,我希望触发动画的开始:

const propVal = getter(props);
const mounted = useRef(true);
useEffect(() => {
  //code to initialize and start animation removed

  //because a started animation uses requestAnimationFrame
  //a component may want to set state for animation while not
  //mounted anymore, prevent the error by setting mounted to false
  return () => (mounted.current = false);
}, [propVal]);//<--if propVal changes the animation starts

Full code is here 完整代码在这里

The propVal could be the id of an item (new item will animate in) or a property called deleted indicating an item has been removed (should animate out) 该PROPVAL可能是一个项目(新项目将在动画)的ID或称为物业deleted指示项目已被删除(应该动画出来)

I am trying to create a smaller code example to re produce the problem I am facing but was not able (yet) to do so. 我正在尝试创建一个较小的代码示例,以重现我面临的问题,但目前还无法(但是)。

The problem is that if I delete an item of then the mounted.current = false (returned callback from useEffects) part gets called even though the component did not unmount. 问题是,如果我删除了其中的一项,那么即使没有卸载组件,也会调用mounted.current = false (从useEffects返回的回调)部分。

When I change the code to return () => false && (mounted.current = false); 当我更改代码以return () => false && (mounted.current = false); basically removing the safeguard to prevent unmounted components from animating then deleting an item will animate without error. 基本上删除保护措施以防止未安装的组件动起来,然后删除一个项目即可进行动画制作而不会出错。 This tells me that the component is not unmounted but somehow the returned callback for onUnmount is called. 这告诉我该组件未卸载,但以某种方式调用了onUnmount返回的回调。

Sorry for not (yet) being able to provide a simpler reproduction of this problem. 很抱歉,目前还不能提供更简单的方法。 Maybe someone knows why the callback would be called when the component is obviously not unmounted (removing the safe guard does not cause errors and animates as expected) 也许有人知道为什么显然没有卸载该组件时会调用该回调(删除安全防护不会导致错误并按预期进行动画处理)

If I understood correctly, you should set the mounted ref to true in the effect. 如果我理解正确,则应将安装的ref设置为true。 Otherwise, any change to the propVal dependency will stop recurAnimate from working. 否则,对propVal依赖项的任何更改将使recurAnimate停止工作。

Say the value changes from an id => to "deleted", The cleanup effect runs but the component isn't unmounted, then the effect runs again with the new value, but the isMounted ref keeps recurAnimate locked. 假设值从id =>更改为“已删除”,将运行清除效果,但未卸载组件,然后使用新值再次运行效果,但是isMounted ref保持recurAnimate锁定。

edit: The cleanup function returned from the effect !== componentWillUnmount. 编辑:从效果!== componentWillUnmount返回的清理函数。 It will run every time the effect will be rerun, and then lastly when the component unmounts. 它将在每次重新运行效果时运行,最后在组件卸载时运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM