3

我将Redux其用作Flux替代方案并React用于视图层。我的应用程序React与方法Redux绑定react-redux connect()。在运行应用程序时,它会在组件挂载并且 redux 返回正确状态时调度操作。但是redux-logger,商店已使用新状态更新的控制台中的日志,在组件中检查this.props.session时仍显示旧状态。我猜我没有connect正确使用该方法,但是我也无法定义它的问题。有谁知道发生了什么?

容器/应用

'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;
4

2 回答 2

5

问题在于从存储session中记录变量的方式。props当您调度操作以更新状态时,它会同步更新存储,这就是为什么您在直接登录时会看到存储已更新的原因。但是,react-redux 将无法更新 props,直到调用componentWillMount完成并且 React 有机会赶上并使用新状态重新渲染组件。如果您稍后发送操作componentWillMount并记录该session道具,您将看到它已更改以反映该操作。

于 2015-10-05T12:18:33.663 回答
0

看起来您没有更改减速器中动作处理程序的状态。只有case除了return state代码说之外state.update('isSessionValid', () => false)——它返回什么?如果它和之前的状态是同一个对象,redux 不会因为不变性约定而改变任何东西。

于 2015-10-05T10:30:43.037 回答