npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-redux-store

v0.4.0

Published

Next integration with Redux

Downloads

84

Readme

next-redux-store

npm

Integration of Next.js with Redux.

Features:

  • State can be passed to the client just once on the first render.
  • It's also possible to load page-level state data between page transitions.
  • SSG and SSR work great.

Installation

npm i next-redux-store

Demo

Demo with RTK Query / Source

Usage

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

const store = makeStore()

export default function _App(appProps: AppProps<any>) {
  const { Component, pageProps } = appProps;
  return (
    <StoreProvider {...{ store, appProps }}>
      <Component {...pageProps} />
    </StoreProvider>
  );
}
  1. Optionally, configure your custom Document to provide initial state for the whole app. It will be populated to the store once on the first page render.
import { createGetInitialProps } from 'next-redux-store/server';
import { makeStore } from 'modules/makeStore';

const getInitialState = async (ctx, appProps) => {
  const store = makeStore();
  // Fill the store considering to the arguments: Document context and App props
  // See example (https://github.com/fakundo/next-redux-store/blob/master/packages/docs/pages/_document.tsx#L14)
  return store.getState();
}

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

  render() {
    ...
  }
}
  1. Optionally, you can also provide state data for some pages with their props (just like next-redux-wrapper works). You can return that state inside getStaticProps / getServerSideProps functions.
import { serverSideState } from 'next-redux-store/server';

export const getStaticProps = async () => {
  const store = makeStore();
  // Fill the store
  const initialState = store.getState();
  return {
    props: {
      ...serverSideState(initialState),
    },
  };
}
  1. Don't forget to configure your reducers to handle the HYDRATE action.
import { createApi } from '@reduxjs/toolkit/query/react';
import { HYDRATE } from 'next-redux-store';

const pokemonApi = createApi({
  reducerPath: 'pokemonApi',
  ...
  extractRehydrationInfo: (action, { reducerPath }) => {
    if (action.type === HYDRATE) {
      return action.payload[reducerPath];
    }
  },
});

API

import { StoreProvider } from 'next-redux-store'

interface StoreProviderProps extends Omit<ProviderProps, 'store'> {
  store: Store<any>;
  appProps: AppProps<any>;
}
declare class StoreProvider extends Component<StoreProviderProps> {}

import { HYDRATE } from 'next-redux-store';

HYDRATE returns name of the hydration action.

import { createGetInitialProps } from 'next-redux-store/server';

Function createGetInitialProps accepts a function that takes Document context and App props as parameters and returns the initial state of the store for the whole app.

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

import { serverSideState } from 'next-redux-store/server';

Function serverSideState accepts state of the store and returns page props.

License

MIT