【发布时间】:2018-08-22 11:52:19
【问题描述】:
我的 App.js 中有两个容器,状态保存在 Redux 存储中。
<StartGameContainer/>
<GameContainer/>
我的应用程序的状态有一个名为 "gameStatus" 的属性,该属性设置为 false。如果为真,我正在尝试使用此属性在GameContainer 中呈现我的组件。当我单击 StartGameContainer 中的“开始”按钮时,此属性设置为 true。
当 App 最初呈现时,它会注意到此属性为 false。当我单击开始按钮时,它对呈现来自GameContainer 的内容没有任何影响。我怎样才能做到这一点或弄错了这个布局?
编辑
GameContainer.js
const mapStateToProps = state => (
{
board: state.boardGame.board,
gameStatus: state.boardGame.gameStatus
}
);
const mapDispatchToProps = dispatch => {
return {
initGame: () => {
dispatch(allActions.initGame())
},
selectCol : (val) => {
dispatch(allActions.dropTile(val))
}
}
};
const GameContainer = connect(mapStateToProps, mapDispatchToProps)(GridCells);
class GridCells extends Component {
componentDidMount() {
this.props.initGame();
}
render() {
if(this.props.gameStatus){
return (
<div className="game">
<table>
<thead>
</thead>
<tbody>
{this.props.board.map((row, i) => (
<RowCells key={i} row={row} select={this.props.selectCol}/>
))}
</tbody>
</table>
</div>
)
}else{
return(<div></div>)
}
}
}
StartGameContainer.js
const mapDispatchToProps = dispatch => {
return{
pickPlayer: (currPlayer) => {
dispatch(allActions.setPlayer(currPlayer))
}
}
};
const StartGameContainer = connect(null, mapDispatchToProps)(StartGame);
class StartGame extends Component{
constructor(props){
super(props);
this.players = ['myself', 'service'];
this.selectedVal = 1;
}
selectedPlayer(event){
this.selectedVal = event.target.value === 'myself' ? 1 : 2;
}
render(){
let options = this.players.map((val) => {
return (<option key={val} value={val}>{val}</option>)
});
return(
<div className='startGame'>
<select name="players" id="players" onChange={this.selectedPlayer.bind(this)}>
{options}
</select>
<button onClick= {() => {this.props.pickPlayer(this.selectedVal)}}>Start Game</button>
</div>
)
}
}
【问题讨论】:
-
请附上GameContainer的代码
-
@ChrisCousins 在原始问题中编辑
-
具体问题是什么?您目前对问题的技术描述基本上是“它不起作用”。是商店更新了,但连接的组件没有重新渲染?或者该操作已调度,但商店没有得到更新?有很多地方可能会发生此问题,必须缩小范围。
标签: javascript reactjs redux