我使用Redux
作爲Flux
替代方案,而React
作爲視圖層。我的應用程序的React
和Redux
與react-redux
connect()
方法綁定。 運行應用程序時,它會在組件掛載並且redux返回正確的狀態時調度該操作。但redux-logger
登錄到商店用新狀態更新的控制檯中,在組件中檢查this.props.session
時它仍顯示舊狀態。我猜測我沒有正確使用connect
方法,但是我也無法定義它的問題。有人有一個想法是怎麼回事?React-redux,連接功能不會更新新數據的狀態
容器/應用
'use strict';
import React from 'react';
import {connect} from 'react-redux';
import {fetchUserSession} from 'actions/SessionActions';
class App extends React.Component {
constructor(props) {
super(props);
}
componentWillMount() {
const {dispatch, session} = this.props;
dispatch(fetchUserSession());
console.log(session);
// logs:
// Object {currentUserId: null, errorMessage: null, isSessionValid: null}
// store is bound to window, and the initial state is ImmutabeJS object
console.log(window.store.getState().session.toJS());
// logs:
// Object {currentUserId: null, errorMessage: null, isSessionValid: false}
// as you might noticed the isSessionValid is changed to false
}
render() {
// html here
}
}
function mapStateToProps(state){
return {
session: state.session.toJS()
};
}
export default connect(mapStateToProps)(App);
動作/ Actions.js
'use strict';
import fetch from 'isomorphic-fetch';
export const SESSION_REQUEST = 'SESSION_REQUEST';
export const SESSION_SUCCESS = 'SESSION_SUCCESS';
export function requestSession() {
return {
type: SESSION_REQUEST
};
}
export function receiveSession(user) {
return {
type: SESSION_REQUEST,
user
};
}
export function fetchUserSession() {
return dispatch => {
dispatch(requestSession());
return fetch(`http://localhost:5000/session`)
.then((response) => {
if (response.status === 404) {
dispatch(raiseSessionFailure(response));
}
return response.json();
})
.then(userData => dispatch(receiveSession(userData)));
};
}
減速器/ SessionReducer.js
'use strict';
import {fromJS} from 'immutable';
// UPDATE!!!
// here is the initial state
const initialState = fromJS({
currentUserId: null,
errorMessage: null,
isSessionValid: null
});
function sessionReducer(state = initialState, action) {
switch (action.type) {
case 'SESSION_REQUEST':
return state.update('isSessionValid',() => false);
case 'SESSION_SUCCESS':
console.log('Reducer: SESSION_SUCCESS');
return state;
case 'SESSION_FAILURE':
console.log('Reducer: SESSION_FAILURE');
return state;
default:
return state;
}
}
export default sessionReducer;
級減速器/ RootReducer
'use strict';
import {combineReducers} from 'redux';
import sessionReducer from 'reducers/SessionReducer';
const rootReducer = combineReducers({
session: sessionReducer
});
export default rootReducer;
你能顯示你的狀態是什麼樣子嗎?我認爲,因爲你沒有合併減速器,它看起來就像'initialState',你是否也可以包含這個呢? – Clarkie
嗨@Clarkie我更新了減速機,並把我如何結合減速機 – Max
你有沒有嘗試讓它在沒有使用immutableJS庫的情況下工作?我只用過[immutability helpers](https://facebook.github.io/react/docs/update.html)中的redux反應 – Clarkie