Javascript 基于逻辑React/Redux呈现容器
我的App.js中有两个容器,状态在Redux商店中维护Javascript 基于逻辑React/Redux呈现容器,javascript,reactjs,redux,Javascript,Reactjs,Redux,我的App.js中有两个容器,状态在Redux商店中维护 <StartGameContainer/> <GameContainer/> StartGameContainer.js const mapDispatchToProps = dispatch => { return{ pickPlayer: (currPlayer) => { dispatch(allActions.setPlayer(currPl
<StartGameContainer/>
<GameContainer/>
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>
)
}
}
const mapDispatchToProps=dispatch=>{
返回{
pickPlayer:(currPlayer)=>{
调度(allActions.setPlayer(currPlayer))
}
}
};
const StartGameContainer=connect(null,mapDispatchToProps)(StartGame);
类startName扩展组件{
建造师(道具){
超级(道具);
this.players=[‘我自己’、‘服务’];
此.selectedVal=1;
}
所选播放器(事件){
this.selectedVal=event.target.value==“我自己”?1:2;
}
render(){
让选项=this.players.map((val)=>{
返回({val})
});
返回(
{options}
{this.props.pickPlayer(this.selectedVal)}>开始游戏
)
}
}
不完全确定您的Redux逻辑是什么样的。。。但是打赌你的问题来自于对工作原理的误解
尝试将两行连接代码放在类定义下面:
class StartGame extends Component{ ... }
const StartGameContainer = connect(null, mapDispatchToProps)(StartGame)
查看这篇关于如何提升JS类但未初始化的文章
“在定义类之前使用类从来都不是一个好主意”
下面是一个简化的示例,可以帮助您更好地理解为什么代码没有抛出错误,但也不能正常工作。考虑这个片段:
函数logYourClass(){
console.log(您的类)
var yourClass='所有类详细信息'
}
logYourClass()
请包含GameContainer的代码。@ChrisCousins在原始问题中编辑过具体问题是什么?您目前对问题的技术描述基本上是“它不工作”。是存储更新了,但连接的组件没有重新渲染吗?或者操作已调度,但存储区未更新?这个问题可能会在很多地方发生,必须缩小范围。@RRP这个问题解决了吗?不要在没有反馈的情况下悄悄溜走
class StartGame extends Component{ ... }
const StartGameContainer = connect(null, mapDispatchToProps)(StartGame)