我正在使用路由创建一个React应用程序,并将其当前设置为
<Provider store={store}>
<Router>
<Switch>
<Route exact path="/" component={TourneyOverview}>
</Route>
<Route exact path='/game/:gameIndex' component={GameScore}> </Route>
</Switch>
</Router>
</Provider>
如果我进入'/ game /:gameIndex',GameScore组件会正确加载,但是当我进入'/'时,GameScore组件会出现未定义的错误。我不确定是什么原因引起的错误,我是否需要做些什么来确保该组件准备就绪,即使该组件未包含在当前路由中。
编辑:
function mapStateToProps(state) {
return {
games: state.games
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators(changeGameScore, dispatch);
}
const GameScore = connect(mapStateToProps, mapDispatchToProps)(Game);
export default GameScore;
const Game = ({games}) => {
return (
<div className="GameContainer">
<div className="TeamContainer">
{games.round1[0].team1Name}
<div>
{games.round1[0].team1Score}
</div>
</div>
<div className="TeamContainer">
{games.round1[0].team2Name}
<div>
{games.round1[0].team2Score}
</div>
</div>
</div>
);
}
export default Game;
游戏减速器:
function games(state = [], action) {
return state;
}
export default games;
减根剂
const rootReducer = combineReducers({teams,
games});
export default rootReducer;