React-redux componentDidUpdate()不起作用

时间:2019-12-15 13:44:38

标签: reactjs redux react-redux

当Redux状态更改时,

componentDidUpdate()不会触发。
有时redux用动作INIT_GAME初始化游戏时,有时componentDidUpdate没有触发。
当玩家进入游戏时,网络套接字向他发送有关当前游戏和redux的信息,称为INIT_GAME动作,并更改空白游戏状态以更正当前状态。

Game.jsx

import React, { PureComponent } from 'react';
import { connect } from 'react-redux';

class Game extends PureComponent {
   componentDidUpdate() {... when INIT_GAME fired, componentDidUpdate sometimes didn't react for this ...}
   render() { return(...) }
}

const mapStateToProps = state => ({
    game: state.game
});

export default connect(mapStateToProps, { })(Game);

gameReducer.js

import { INIT_GAME, PLAY_GAME, TIMER_GAME, ROLL_GAME, CLOSE_GAME } from '../actions/gameActions/types';

const initialState = {
    status: "closed",
    time: null,
    players: []
};

export default function(state = initialState, action) {
    switch (action.type) {
        case INIT_GAME:
            return {
                ...state,
                game: action.payload,
                status: action.payload.roll !== null ? "rolling" : "playing"
            }
        case PLAY_GAME:
            return {
                ...state,
                status: "playing",
                players: [
                    ...state.players,
                    action.payload
                ]
            }
        case TIMER_GAME:
            return {
                ...state,
                time: action.payload,
                status: "playing"
            }
        case ROLL_GAME:
            return {
                ...state,
                status: "rolling"
            }
        case CLOSE_GAME:
            return {
                time: null,
                status: "closed"
            }
        default:
            return state
    }
}

0 个答案:

没有答案