[英]React-redux, connect function does not update the state with the new data
I am using Redux
as a Flux
alternative and React
for the view layer. 我使用
Redux
作为Flux
替代品,并使用React
作为视图层。 My application's React
and Redux
are bound with react-redux
connect()
method. 我的应用程序的
React
和Redux
与react-redux
connect()
方法绑定。 When running the application it dispatches the action when the components mounts and the redux returns the correct state. 运行应用程序时,它会在组件安装时调度操作,并且redux返回正确的状态。 However the
redux-logger
logs in the console that the store has updated with the new state, in the component when checking this.props.session
it still shows the old state. 但是,
redux-logger
在控制台中redux-logger
了商店已使用新状态更新的内容,在检查this.props.session
它仍会显示旧状态。 I am guessing that I'm not using the connect
method correctly, however I can't define the issue with it as well. 我猜我没有正确使用
connect
方法,但我也无法用它来定义问题。 Does anyone have an idea whats going on? 有没有人知道最新情况?
containers/App 容器/应用
'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/Actions.js 动作/ 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)));
};
}
reducers/SessionReducer.js 减速器/ 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;
reducers/RootReducer 减速器/ RootReducer
'use strict';
import {combineReducers} from 'redux';
import sessionReducer from 'reducers/SessionReducer';
const rootReducer = combineReducers({
session: sessionReducer
});
export default rootReducer;
The issue is with the way the session
variable is being logged from props
and from the store. 问题在于从
props
和商店记录session
变量的方式。 When you dispatch the action to update state it is synchronously updating the store, which is why you see that the store has been updated when you log it directly. 当您将操作分派给更新状态时,它会同步更新存储,这就是您在直接记录存储时看到存储已更新的原因。 However, react-redux won't be able to update props until the call to
componentWillMount
has completed and React has had a chance to catch up and rerender the component with the new state. 但是,在调用
componentWillMount
完成并且React有机会赶上并重新呈现具有新状态的组件之前,react-redux将无法更新props。 If you dispatch the action in componentWillMount
and log the session
prop at a later time you will see that it has changed to reflect the action. 如果您在
componentWillMount
调度操作并稍后记录session
道具,您将看到它已更改以反映该操作。
Looks like you don't change state in action handlers in reducer. 看起来你没有在reducer中改变动作处理程序中的状态。 Only
case
with other than return state
code says state.update('isSessionValid', () => false)
— what does it return? 只有
return state
代码以外的case
才说state.update('isSessionValid', () => false)
- 它返回什么? If it's the same object as previous state, redux wouldn't change anything because of immutability convention. 如果它与先前状态是同一个对象,则redux不会因为不变性约定而改变任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.