gpt4 book ai didi

reactjs - 在 NextJS 中的 getServerSideProps 中访问 Redux 存储

转载 作者:行者123 更新时间:2023-12-04 14:28:10 26 4
gpt4 key购买 nike

我想在 getServerSideProps() 中访问我的 redux 存储;在我的 Next.js 应用程序中。我想拥有存储在 Redux Store 中的用户 ID,以便我可以预加载用户数据。我现在正在使用 next-redux-wrapper 并且在客户端它正在工作。但是当我在 getServerSideProps() 中访问我的商店时,它返回初始状态。我检查了几个例子,但我无法让它工作。
我的configureStore.js

import { createStore, applyMiddleware, compose } from "redux";
import reducer from "./../service";
import thunk from 'redux-thunk'
import { createWrapper, HYDRATE} from 'next-redux-wrapper';

const SET_CLIENT_STATE = 'SET_CLIENT_STATE';

const storeEnhancers = typeof window != 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose

const makeStore = ({ isServer }) => {

if (isServer) {

return createStore(reducer, storeEnhancers(applyMiddleware(thunk)) );

} else {

const { persistStore, persistReducer } = require('redux-persist');
const storage = require('redux-persist/lib/storage').default;

const persistConfig = {
key: 'nextjs',
whitelist: ['userReducer'],
storage
};

const store = createStore(
persistReducer(persistConfig, reducer),
storeEnhancers(applyMiddleware(thunk))
);

store.__PERSISTOR = persistStore(store);

return store;
}
};

export const wrapper = createWrapper(makeStore);

export const setClientState = (clientState) => ({
type: SET_CLIENT_STATE,
payload: clientState
});
我的_app.js:
import React from 'react';
import PropTypes from 'prop-types';
import Head from 'next/head';
import { ThemeProvider } from '@material-ui/core/styles';
import CssBaseline from '@material-ui/core/CssBaseline';
import theme from '../styles/theme';
import { useStore } from "react-redux";
import { PersistGate } from 'redux-persist/integration/react';
import {wrapper} from './../redux/configureStore';

const MyApp = ({Component, pageProps}) => {
const store = useStore((state) => state);

React.useEffect(() => {
const jssStyles = document.querySelector('#jss-server-side');
if (jssStyles) {
jssStyles.parentElement.removeChild(jssStyles);
}
}, []);

return (
<React.Fragment>
<Head>
<title>Deeltuinen</title>
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
</Head>
<PersistGate persistor={store.__PERSISTOR} loading={null}>
<ThemeProvider theme={theme}>
<CssBaseline />
<Component {...pageProps} />
</ThemeProvider>
</PersistGate>
</React.Fragment>
);
}

export default wrapper.withRedux(MyApp);

MyApp.propTypes = {
Component: PropTypes.elementType.isRequired,
pageProps: PropTypes.object.isRequired
};
最后是我想获取商店状态的页面:
import React from 'react';
import Layout from './../../components/Layout';
import API from './../../service/api';
import {connect} from "react-redux";
import {wrapper, setClientState} from './../../redux/configureStore';

const AccountSettings = ({data}) => {

return (
<Layout title="Deeltuinen">
// User form
</Layout>
);
}

export const getServerSideProps = wrapper.getServerSideProps(
({store, req, res}) => {
const state = store.getState();
console.log(state);

//const data = await API.getUser(state.userReducer.user.token);
}
);

export default connect(state => state,{setClientState})(AccountSettings);

最佳答案

我的代码:

export const getServerSideProps = wrapper.getServerSideProps(async ({ req, res, store }) => {
const state = store.getState();
console.log('state', state);

return {
props: {},
};
});
`

关于reactjs - 在 NextJS 中的 getServerSideProps 中访问 Redux 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62818195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com