reactjs - React-redux componentDidUpdate() 不起作用
问题描述
当 redux 状态发生变化时,componentDidUpdate() 不会触发。
当 redux 使用动作 INIT_GAME 初始化游戏时,有时 componentDidUpdate 没有触发。
当玩家进入游戏时,websocket 向他发送有关当前游戏的信息,redux 调用 INIT_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
}
}
解决方案
推荐阅读
- java - 如何按字段类型对匹配的文档字段进行分组?
- sql - 如何在同一表组的不同行组之间按 1 值计算
- android - Speech to Text (RecongnitionListener) 更改错误消息的提示
- powerbi - 使用 Power BI 的 REST API 创建数据集并将其与自定义连接器关联
- javascript - 未捕获的类型错误:d3.arc(...).outerRadius(...).innerRadius(...).text 不是函数
- php - 已解决 - Laravel 6 登录在服务器(Heroku)上没有做任何事情
- python - 与python的组合
- python - Python。无法从文件中导入多个函数
- python - 模块“mysite.asgi”在 Heroku 和 Django 应用程序上没有属性“channel_layer”
- javascript - 是否有可能在通过来自另一个网站的重定向打开的 React 网站中捕获响应?