Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

next-redux-store

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-redux-store

Next integration with Redux

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

next-redux-store

npm

Next integration with Redux.

Features:

  • state data is passed to the client once when rendering the first page
  • does not load state between page transitions
  • does not use the HYDRATE action, instead uses preloadedState to configure store on the client once
  • SSG and SSR work great

Installation

npm i next-redux-store

Demo

Demo with RTK Query / Source

Usage

  1. Configure your App to use Redux store
import { makeStore, Provider } from 'modules/redux';
import { createUseStore } from 'next-redux-store';
import { AppProps } from 'next/app';

const useStore = createUseStore(makeStore);

export default function _App({ Component, pageProps, ...rest }: AppProps<any>) {
  const store = useStore(rest);
  return (
    <Provider store={store}>
      <Component {...pageProps} />
    </Provider>
  );
}
  1. Configure your Document to provide initial state for the App
import { makeStore } from 'modules/redux';
import { createGetInitialProps } from 'next-redux-store/createGetInitialProps';

const getInitialState = async (appProps, ctx) => {
  if (!appProps) {
    return undefined;
  }
  const store = makeStore();
  // fill the store considering the App props and Document context
  return store.getState();
}

export default class _Document extends Document {
  static getInitialProps = createGetInitialProps(getInitialState);

  render() {
    ...
  }
}

API

createUseStore

createUseStore creates a React hook that returns a Redux store. It accepts a store creation function with a preloadedState as parameter. The hook accepts App props.

type MakeStore = (preloadedState?: any | undefined) => Store;
const createUseStore: (makeStore: MakeStore) => (appProps: AppProps<any>) => Store;

createGetInitialProps

createGetInitialProps creates a function that returns initial props for the Document, providing initialState of store for the App.

type CreateInitialState = (appProps?: AppProps<any> | undefined, ctx?: DocumentContext) => any;
const createGetInitialProps: (createInitialState: CreateInitialState) => (ctx: DocumentContext) => DocumentInitialProps;

createInitialState can accept two parameters: App props and Document context.

License

MIT

Keywords

FAQs

Package last updated on 05 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc