简体   繁体   English

如何在 Next.js 环境中访问 redux-toolkit 状态(不是初始状态)

[英]how to access redux-toolkit state (not initial state) on Next.js environmemnt

i want to acccess to redux-toolkit's state at Next.js's getStaticProps我想在 Next.js 的 getStaticProps 中访问 redux-toolkit 的状态

(After saving the accessToken in the store I want to access the store from getstaticprops for the api it needs) (在商店中保存 accessToken 后,我想从 getstaticprops 访问它需要的 api 的商店)

so i tried like this所以我试过这样

export default function Page({ state }) {
  console.log(state)
  return <>...</>
}
export const getStaticProps = wrapper.getStaticProps((store) => {
  return async () => {
    const state = store.getState()
    return {
      props: {
        state
      }
    }
  }
})

finally access was successful so i check state on redux devtools and it is there最终访问成功,所以我检查了 redux devtools 上的状态,它就在那里

but state is realistically initial state (empty)但状态实际上是初始状态(空)

redux devtools.img redux devtools.img

console.log.img控制台.log.img

this is my redux toolkit setup on Next.js这是我在 Next.js 上的 redux 工具包设置

//_app.js

import { wrapper } from '../store'

function MyApp({ Component, pageProps }) {
  axios.defaults.withCredentials = true
  return (
    <Layout>
      <Component {...pageProps} />
    </Layout>
  )
}

export default wrapper.withRedux(MyApp)
/store/index.js

import { configureStore } from '@reduxjs/toolkit'
import { createWrapper } from 'next-redux-wrapper'
import logger from 'redux-logger'
import reducer from './modules'

const makeStore = (context) =>
  configureStore({
    reducer,
    middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(/*logger*/),
    devTools: process.env.NODE_ENV !== 'production'
  })

export const wrapper = createWrapper(makeStore, {
  debug: process.env.NODE_ENV !== 'production'
})


/store/modules/index.js

import { combineReducers } from '@reduxjs/toolkit'
import { HYDRATE } from 'next-redux-wrapper'

import timer from './timer'
import user from './user'
import cart from './cart'

const reducer = (state, action) => {
  if (action.type === HYDRATE) {
    return {
      ...state,
      ...action.payload
    }
  }
  return combineReducers({
    user,
    timer,
    cart
  })(state, action)
}
export default reducer

/store/modules/user.js
import { createSlice } from '@reduxjs/toolkit'

const initialState = { userInfo: null, token: null, address: null }

const userSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    getNewToken: (state, action) => {
      state.token = action.payload
    },
    getUserInfo: (state, action) => {
      state.userInfo = action.payload
    },
    getAddress: (state, action) => {
      state.address = action.payload
    }
  }
})

export const { getUserInfo, getNewToken, getAddress } = userSlice.actions
export default userSlice.reducer

getStaticProps run on the server, without any knowledge of what is going on on your client. getStaticProps在服务器上运行,不知道客户端上发生了什么。 This even happens on site generation, which could be anywhere between seconds or years before the user actually uses the page.这甚至发生在站点生成中,这可能发生在用户实际使用页面之前的几秒或几年之间。 You will never have access to the client-side Redux store there - and as you do not know how the client navigated to that page, it will always be a "new" Redux state.您将永远无法访问那里的客户端 Redux 存储 - 由于您不知道客户端如何导航到该页面,因此它将始终处于“新”Redux 状态。

You can dispatch some actions within getStaticProps and then get the resulting state via getState , but it will always be isolated per getStaticProps call.您可以在getStaticProps dispatch一些操作,然后通过getState获取结果状态,但每次getStaticProps调用都会将其隔离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM