我的react组件中有一个redux函数调用,如下所示。
componentDidMount() {
let gameId = this.props.match.params.gameId;
this.props.fetchCurrentGameSession(gameId)
.then(response => {
if (response !== undefined) {
if (!response.error) {
this.setState({
currentSession: {
loading: false,
loaded: true,
error: false,
}
});
}
else {
this.setState({
currentSession: {
loading: false,
loaded: false,
error: true,
}
});
}
}
});
函数的声明:
const mapDispatchToProps = (dispatch) => {
return {
fetchCurrentGameSession: (gameId) => dispatch(loadCurrentGameSession(gameId)),
}
};
export default compose(
connect(
mapStateToProps,
mapDispatchToProps
),
withStyles(styles, {
withTheme: true
}
))(LayoutGameView);
但是有时候,当页面加载时,我得到了错误
未处理的拒绝(TypeError):无法读取未定义的属性'then'
我将在哪里添加该函数以根据redux函数返回的内容来更改状态,以免出现此错误?
编辑:
action.js
:
export const FETCH_CURRENT_GAME_SESSION = '@@game/FETCH_CURRENT_GAME_SESSION';
export const FETCH_CURRENT_GAME_SESSION_SUCCESS = '@@game/FETCH_CURRENT_GAME_SESSION_SUCCESS';
export const FETCH_CURRENT_GAME_SESSION_FAILURE = '@@game/FETCH_CURRENT_GAME_SESSION_FAILURE';
export const loadCurrentGameSession = (gameId) => ({
[RSAA]: {
endpoint: `/api/games/${gameId}/sessions/current`,
method: 'GET',
headers: withAuth({ 'Content-Type': 'application/json' }),
types: [
FETCH_CURRENT_GAME_SESSION, FETCH_CURRENT_GAME_SESSION_SUCCESS, FETCH_CURRENT_GAME_SESSION_FAILURE
]
}
});