【问题标题】:Redux state don't change after dispatch on next jsRedux 状态在下一个 js 调度后不会改变
【发布时间】:2019-02-24 03:56:11
【问题描述】:

我是下一个 js 的新手,我使用 redux-next-wrapper !

问题是我想分派令牌访问,但是当我在 getInitialProps 中执行此操作时,商店在页面呈现后不会更新!

我尝试使用componentDidMount,它可以工作,但是只有在页面渲染后状态才会更新,这使得按钮登录前一秒可见被注销替换!

  componentDidMount () {
    const token_access = Cookies.get('xxxxxxxxxx');
    const token_refresh = Cookies.get('xxxxxxxxxx');
    console.log(token_access);
    if (token_access && token_refresh) {
      const decode = jwt_decode(token_refresh);
      if (decode.exp >= new Date()) {
        this.props.store.dispatch(logout(token_refresh))
      }
      else {
        const decode_access = jwt_decode(token_access);
        if (decode_access.exp >= new Date()) {
          refresh(token_refresh)
        }
        this.props.store.dispatch(userLoggedIn(token_access));
      }
    }
  }

  static async getInitialProps ({Component, ctx}) {
    const token = '12345';
    ctx.store.dispatch(userLoggedIn(token));
    return {
      pageProps: (Component.getInitialProps ? await Component.getInitialProps(ctx) : {})
    }
  }

import { createStore, applyMiddleware } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension';
import thunk from 'redux-thunk';
import rootReducer from '../reducers/index'

export default initialState => createStore(
  rootReducer,
  composeWithDevTools(
    applyMiddleware(thunk)
  )
);

有没有办法在页面渲染之前调度和加载商店?

谢谢你的回答

【问题讨论】:

    标签: redux redux-thunk next.js


    【解决方案1】:

    我通过更改商店来解决此问题!希望这对某人有帮助:)

    export const initStore = (initialState = {} ) => {
      return createStore(rootReducer,
        initialState, composeWithDevTools(applyMiddleware(thunk)))
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 2016-09-27
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 2017-11-13
      相关资源
      最近更新 更多