0

我一直在尝试使用 nextjs 和 redux 创建一个入门工具包。我使用 next-redux-wrapper HOC 通过 getInitialProps 访问服务器端的存储。它成功地工作。但是,当我尝试在 render 方法中访问客户端上的 Store 时, store 被获取为 undefined。我认为问题出在_app.tsx。如果你有一个想法,我把回购网址:https ://github.com/agursoyy/next-redux-ts

import React from 'react';
import { NextPageContext } from 'next';
import App, { AppInitialProps, AppContext, Container } from 'next/app';
import withRedux from 'next-redux-wrapper';
import { Provider } from 'react-redux';
import { Store, RootState, wrapper } from '../stores';
import { login } from '../stores/auth/actions';

interface IProps extends AppInitialProps {
  store: Store;
}

class MyApp extends App<IProps> {
  static async getInitialProps({ Component, ctx }: AppContext) {
    const { store } = ctx;
    store.dispatch(login({ email: 'email', password: 'password' }));
    console.log(store.getState());
    return { pageProps: {} };
  }
  render() {
    const { Component, pageProps, store } = this.props;
    console.log(store);
    return (
      <Component {...pageProps} />
    );
  }
}

export default wrapper.withRedux(MyApp);
4

0 回答 0