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-tools/redux

v0.0.5

Published

generate initRedux and useRedux to use in next js

Downloads

10

Readme

Next js with Redux

Default and only export is a generator that you pass your client creation function into. It returns two functions that allow you to access the redux store in the context of a react component or any other context that may have to do with pre-rendering on the server or client in next js.

  1. initRedux - Function that takes a single optional argument which is initial state to hydrate the cache. This function can be used inside getServerSideProps, getStaticProps, and getInitialProps.
  2. useRedux - Hook version of initRedux. Used in a react component, typically in pages/_app.js to supply the Redux Provider.

This uses the exact concepts from the latest next js examples git repository for using redux with next js. https://github.com/vercel/next.js/tree/canary/examples/with-redux It is packaged up for convienience so you don't have to keep having to re-writing this functionality and only worry about passing in your store creation function and using the redux.

Example Usage

  1. create a file that you will export the two generated functions from your store creation function. example path: ~/lib/redux.js (where ~ is project root)

    import { createStore } from "redux";
    import nextRedux from "@next-tools/redux";
    import reducer from "somewhere";
    
    export const { initRedux, useRedux } = nextRedux(function (initState) {
      return createStore(reducer, initState);
    });
  2. Provider in pages/_app.js

    import { useRedux } from "~/lib/redux";
    import { Provider } from "react-redux";
    
    const App = ({ Component, pageProps }) => {
      const reduxStore = useRedux(pageProps.initReduxState);
    
      return (
        <Provider store={reduxStore}>
          <Component {...pageProps} />
        </Provider>
      );
    };
    
    export default App;
  3. Usage for ssr in any of the 3 available functions getServerSideProps, getStaticProps, or getInitialProps on a next js page

    import { initRedux } from "~/lib/redux";
    import fetchData from "fetchData";
    import actions from "somewhere/actions";
    
    const Page = ({ something }) => {
      return <div>...</div>;
    };
    
    // connect to anything with pre-rendered state. here just showing page component for simple example
    export default connect(({ something }) => ({ something }), actions)(Page);
    
    Page.getInitialProps = async function () {
      const store = initRedux();
      const data = await fetchData();
    
      store.dispatch(actions.doSomething(data));
      // only pass an init state if you are on the server
      // this makes sure that you don't re-create the store every client-side transition
      return {
        initReduxState:
          typeof window === "undefined" ? store.getState() : undefined
      };
    };
    import { initRedux } from "~/lib/redux";
    import fetchData from "fetchData";
    import actions from "somewhere/actions";
    
    const Page = ({ something }) => {
      return <div>...</div>;
    };
    
    // connect to anything with pre-rendered state. here just showing page component for simple example
    export default connect(({ something }) => ({ something }), actions)(Page);
    
    export async function getServerSideProps() {
      const store = initRedux();
      const data = await fetchData();
    
      store.dispatch(actions.doSomething(data));
    
      return {
        props: {
          initReduxState: store.getState()
        }
      };
    }
    import { initRedux } from "~/lib/redux";
    import fetchData from "fetchData";
    import actions from "somewhere/actions";
    
    const Page = ({ something }) => {
      return <div>...</div>;
    };
    
    // connect to anything with pre-rendered state. here just showing page component for simple example
    export default connect(({ something }) => ({ something }), actions)(Page);
    
    export async function getStaticProps() {
      const store = initRedux();
      const data = await fetchData();
    
      store.dispatch(actions.doSomething(data));
    
      return {
        props: {
          initReduxState: store.getState()
        }
      };
    }