next js redux code example

Example 1: next js create store

//Now, we create our _app.js file in pages directory. Add the following lines of code.

import React from "react";
import App, { Container } from "next/app";
import { Provider } from "react-redux";
import withRedux from "next-redux-wrapper";
import { initStore } from "../state/store";


class MyApp extends App {
   static async getInitialProps({ Component, ctx }) {
       const pageProps = Component.getInitialProps
           ? 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(initStore)(MyApp);

Example 2: next js using redux

check my link github for example usage -> https://github.com/restuwahyu13/nextjs-with-redux