我可以在reducer中调度动作吗?

reactjs React.js

TonyEvaL

2020-03-11

是否可以在减速器本身中调度动作?我有一个进度栏和一个音频元素。目标是在音频元素中的时间更新时更新进度条。但是我不知道在哪里放置ontimeupdate事件处理程序,或者如何在ontimeupdate的回调中分派动作来更新进度条。这是我的代码:

//reducer

const initialState = {
    audioElement: new AudioElement('test.mp3'),
    progress: 0.0
}

initialState.audioElement.audio.ontimeupdate = () => {
    console.log('progress', initialState.audioElement.currentTime/initialState.audioElement.duration);
    //how to dispatch 'SET_PROGRESS_VALUE' now?
};


const audio = (state=initialState, action) => {
    switch(action.type){
        case 'SET_PROGRESS_VALUE':
            return Object.assign({}, state, {progress: action.progress});
        default: return state;
    }

}

export default audio;

第566篇《我可以在reducer中调度动作吗?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
阿飞西里 2020.03.11

redux-loop从Elm那里得到提示,并提供了这种模式。

老丝Tony 2020.03.11

您可以尝试使用redux-saga之类的库它提供了一种非常干净的方式来排序异步功能,触发动作,使用延迟等。它非常强大!

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android