I have this home page
const Home: NextPage = () => {
return (<> <Link href='/2'><a>Go to page 2</a></Link> </>)
}
Home.getInitialProps = wrapper.getInitialPageProps(
({ dispatch }) =>
async () => {
await dispatch(addInitialSources('book'))
await dispatch(addCategoriesMenu('book'))
}
);
export default Home
Note the two actions dispatched. That sets the initial state of the entire app.
When we go to [id]/index.tsx
, I need the store to have the same initial state as requested from the home page. I could just copy the above
const SecondPage: NextPage = () => {
return (<> <Link href='/'><a>Go Home</a></Link> </>)
}
SecondPage.getInitialProps = wrapper.getInitialPageProps(
({ dispatch }) =>
async () => {
await dispatch(addInitialSources('book'))
await dispatch(addCategoriesMenu('book'))
}
);
export default SecondPage
Is there a way to call/set those dispatches in a way that they aren't being called on every page?
This is the pages/_app.tsx
setup if needed.
import 'tailwindcss/tailwind.css';
import type { AppProps } from 'next/app'
import { wrapper } from "@store/store"
function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
export default wrapper.withRedux(MyApp)