简体   繁体   English

React Redux- Store 更新前超时

[英]React Redux- Timeout before Store Update

I'm working on getting animations with anime js into my react redux app.我正在努力将带有动漫 js 的动画添加到我的 react redux 应用程序中。 I have a notifications objects in the store, and when the user clicks delete a notification is deleted, and the store updates.我在商店中有一个通知对象,当用户单击删除时,通知被删除,商店更新。 However, I need to wait a couple hundred milliseconds to let the fade-out animation to play.但是,我需要等待几百毫秒才能让淡出 animation 播放。 I tried setting a time out before calling the action, with no luck.我尝试在调用操作之前设置超时,但没有运气。

handleDeleteNoti = notification => {
    setTimeout(()=>{
        anime({
        targets: this.notiRef,
        opacity: [1,0],
        duration: 5000,
        })
    }, 5000);
    this.props.clearNotification(notification._id);
}

I also tried setting a timeout in the reducer with no success either.我还尝试在减速器中设置超时,但也没有成功。

case CLEAR_NOTIFICATION:
    const newData = state.data.filter(notification=>notification._id!==action.payload);
    setTimeout(()=>{
        return {
            ...state, 
            data: newData
        }}, 400);

I guess my question really boils down to a reverse setTimeout, where the function is called while the timeout is happening.我想我的问题真的归结为反向 setTimeout,在超时发生时调用 function。 Is there any way to solve this?有没有办法解决这个问题? Thanks!谢谢!

Also, I have my animation in componentWillUnmount另外,我在componentWillUnmount中有我的 animation

The answer was actually pretty simple, I just called the animation and set a timeout until the action was called:答案其实很简单,我只是调用了 animation 并设置了一个超时,直到调用该操作:

handleDeleteNoti = notification => {
    anime({
        targets: this.notiRef,
        opacity: [1,0],
        duration: 500,
    });

    setTimeout(()=>{this.props.clearNotification(notification._id)}, 5000)
    // this.props.clearNotification(notification._id);
}

You can probably achieve that by calling clearNotification after the timeout.您可能可以通过在超时后调用clearNotification来实现。

anime({targets: this.notiRef,opacity: [1,0],duration: 5000});
setTimeout(()=>{this.props.clearNotification(notification._id)}, 5000);

Also I recommend not using side effects in the reducer because that is an anti-pattern .另外我建议不要在减速器中使用副作用,因为这是一种反模式

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

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