使用成功操作更新Redux状态后,React组件未刷新

时间:2018-12-02 04:00:27

标签: javascript reactjs redux

React / Redux组合的新功能,并尝试解决问题。

当用户首次访问或登录时,会发出fetch_user api请求。目的是使页面根据其登录状态显示不同。在redux开发工具中,我可以看到状态已更新,并且在初始状态后将字段填充为“ auth”,但是,当我位于应用程序的子组件中时,该值被视为未定义。如果您需要更多信息,请告诉我。提前致谢。

    // app.js
            const initialState = {};
    const history = createHistory();
    const store = configureStore(initialState, history);

    const MOUNT_NODE = document.getElementById('app');

    const render = messages => {
      ReactDOM.render(
        <Provider store={store}>
          <LanguageProvider messages={messages}>
            <ConnectedRouter history={history}>
              <App />
            </ConnectedRouter>
          </LanguageProvider>
        </Provider>,
        MOUNT_NODE,
      );
    };

    // index.js
            class App extends React.Component {
      componentDidMount() {
        console.log('here');
        this.props.fetchUser();
      }
      render() {
        return (
          <ThemeWrapper>
            <AppContext.Consumer>
          ..... 
        App.propTypes = {
            fetchUser: PropTypes.any.isRequired
        };

   export default withRouter(connect(null, actions)(App));


  import { FETCH_USER } from '../actions/types';

  export default function (state = null, action) {
    switch (action.type) {
      case FETCH_USER:
        console.log('1');
        return action.payload || false;
      default:
        return state;
    }
  }
    // actions
    export const fetchUser = () => async dispatch => {
      const res = await axios.get('/api/current_user');
      // res is the output of the axios request
      dispatch({ type: FETCH_USER, payload: res.data });
    };

// Banner.js - auth = undefined
render() {
    console.log(this.props);
// === auth = undefined. I may be accessing it incorrectly


const mapStateToProps = state => ({
    gradient: state.getIn([reducerUI, 'gradient']),
    chat: state.getIn([chatUI, 'chatSelected']),
    auth: state.auth
  });

  const BannerMaped = connect(
    mapStateToProps,
  )(Banner);

  // configure store

      export default function configureStore(initialState = {}, history) {
        // Create the store with two middlewares
        // 1. sagaMiddleware: Makes redux-sagas work
        // 2. routerMiddleware: Syncs the location/URL path to the state
        const middlewares = [sagaMiddleware, routerMiddleware(history), reduxThunk];

        const enhancers = [applyMiddleware(...middlewares)];

        // If Redux DevTools Extension is installed use it, otherwise use Redux compose
        /* eslint-disable no-underscore-dangle, indent */
        const composeEnhancers =
          process.env.NODE_ENV !== 'production' &&
          typeof window === 'object' &&
          window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
            ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
                // TODO Try to remove when `react-router-redux` is out of beta, LOCATION_CHANGE should not be fired more than once after hot reloading
                // Prevent recomputing reducers for `replaceReducer`
                shouldHotReload: false,
              })
            : compose;
        /* eslint-enable */
        const store = createStore(
          createReducer(),
          fromJS(initialState),
          composeEnhancers(...enhancers),
        );

        // Extensions
        store.runSaga = sagaMiddleware.run;
        store.injectedReducers = {}; // Reducer registry
        store.injectedSagas = {}; // Saga registry

        // Make reducers hot reloadable, see http://mxs.is/googmo
        if (module.hot) {
          module.hot.accept('./reducers', () => {
            store.replaceReducer(createReducer(store.injectedReducers));
          });
        }

        return store;
      }

enter image description here enter image description here

3 个答案:

答案 0 :(得分:0)

Redux存储更新被映射到单个组件,而不是整个应用程序。

此行意味着,在更新商店时,只会重新渲染Banner组件,而不是整个应用程序。

const BannerMaped = connect(
  mapStateToProps,
)(Banner);

因此,无论您的Banner组件在哪里,每次fetchUser()响应成功并更新商店时,都只会重新呈现您的Banner组件。如果您需要重新渲染其他组件,则它们还应该使用相应的mapStateToProps订阅存储。

答案 1 :(得分:0)

您还需要在connect方法中传递调度动作。就您而言,您已经执行了fetchUser()操作。因此,您可以像这样在connect方法中传递它:

const BannerMaped = connect(
  mapStateToProps,
  fetchUser
)(Banner);  

我认为这会有所帮助。

答案 2 :(得分:0)

我做的一切正确,只是没有正确地访问状态对象。盯着这个有点太久了。

const mapStateToProps = state => ({
gradient: state.getIn([reducerUI, 'gradient']),
chat: state.getIn([chatUI, 'chatSelected']),
auth: state.getIn(['auth'])

});