在_app.js(nextjs)中接收新道具?

时间:2020-01-28 10:38:11

标签: reactjs next.js

我想说的是,如果我在_app的componentDidMount中调度一个动作,那么我会在_app中收到此动作的新道具吗?如果是,如何?谢谢

import React from 'react'
import {Provider} from 'react-redux'
import App, {Container} from 'next/app'
import withRedux from 'next-redux-wrapper'
import withReduxSaga from 'next-redux-saga'
import configureStore from './configure-store'

class ExampleApp extends App {
  static async getInitialProps({Component, ctx}) {
    let pageProps = {}
     //This a action with redux-saga
     ctx.store.dispatch(doSomething)

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx)
    }

    return {pageProps}
  }

  render() {
    const {Component, pageProps, store} = this.props
    return (
      <Container>
        <Provider store={store}>
          <Component {...pageProps} />
        </Provider>
      </Container>
    )
  }
}

export default withRedux(configureStore)(withReduxSaga(ExampleApp))

在此代码中,如何以及在哪里恢复操作结果?

我的configureStore:

import reducers from "./services/reducers";
import { createStore, applyMiddleware, compose } from "redux";
import createSagaMiddleware from "redux-saga";
import IndexSaga from "./services/sagas";

export const configureStore = (preloadedState, { isServer, req = null }) => {
  const composeEnhancers =
    (typeof window !== "undefined" &&
      window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
    compose;
  const sagaMiddleware = createSagaMiddleware();

  const store = createStore(
    reducers,
    preloadedState,
    composeEnhancers(applyMiddleware(sagaMiddleware))
  );

  if (req || !isServer) {
    store.sagaTask = sagaMiddleware.run(IndexSaga);
  }
  return store;
};

1 个答案:

答案 0 :(得分:0)

然后就回到您的商店,并从那里带来一些更新的价值。

....
const store = configureStore.getState();