Can I dispatch an action in reducer?

前端 未结 4 850
暖寄归人
暖寄归人 2020-11-28 01:44

is it possible to dispatch an action in a reducer itself? I have a progressbar and an audio element. The goal is to update the progressbar when the time gets updated in the

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 02:08

    Dispatching an action within a reducer is an anti-pattern. Your reducer should be without side effects, simply digesting the action payload and returning a new state object. Adding listeners and dispatching actions within the reducer can lead to chained actions and other side effects.

    Sounds like your initialized AudioElement class and the event listener belong within a component rather than in state. Within the event listener you can dispatch an action, which will update progress in state.

    You can either initialize the AudioElement class object in a new React component or just convert that class to a React component.

    class MyAudioPlayer extends React.Component {
      constructor(props) {
        super(props);
    
        this.player = new AudioElement('test.mp3');
    
        this.player.audio.ontimeupdate = this.updateProgress;
      }
    
      updateProgress () {
        // Dispatch action to reducer with updated progress.
        // You might want to actually send the current time and do the
        // calculation from within the reducer.
        this.props.updateProgressAction();
      }
    
      render () {
        // Render the audio player controls, progress bar, whatever else
        return 

    Progress: {this.props.progress}

    ; } } class MyContainer extends React.Component { render() { return } } function mapStateToProps (state) { return {}; } return connect(mapStateToProps, { updateProgressAction })(MyContainer);

    Note that the updateProgressAction is automatically wrapped with dispatch so you don't need to call dispatch directly.

提交回复
热议问题