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 🙏

© 2026 – Pkg Stats / Ryan Hefner

connect-initial-props

v1.0.0

Published

A decorator for Next.js and React-Redux apps to connect `getInitialProps` to `state` and `dispatch`

Downloads

64

Readme

connect-initial-props

A decorator for Next.js and React-Redux apps to connect getInitialProps to state and dispatch.

Why?

When using react-redux on a Next.js project, the static getInitialProps method is executed before react-redux's had been able to map state and dispatch to props.

The result is that accessing the dispatching actions or later accessing the state in getInitialProps is complicated.

Using this decorator you will be able to access mapped state and dispatch actions just as you would access them in a non-static method.

Example

Without connect-initial-props:

static async getInitialProps(ctx) {
  const { store, query, res } = ctx;
  await store.dispatch(getItems({ page }));

  const { totalPages } = mapStateToProps(store.getState());

  if (page <= 1 || page > totalPages) {
    res.redirect(301, '/?page=1');
    return {};
  }

  return { page };
}

With connect-initial-props's help:

@connectInitialProps(mapStateToProps, mapDispatchToProps)
static async getInitialProps(ctx, props) {
  const { res } = ctx;
  const { page, getItems } = props;

  // async call to load items from api
  await getItems({ page });

  // props got updated with up-to-date state after async request
  const { totalPages } = props;

  if (page <= 1 || page > totalPages) {
    res.redirect(301, '/?page=1');
    return {};
  }
}

Full Component Example

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import connectInitialProps from 'connect-initial-props';
import { createStructuredSelector } from 'reselect';
import { selectItems, selectTotalPages } from './selectors';
import { getItems } from './slice';

const mapStateToProps = createStructuredSelector({
  items: selectItems,
  totalPages: selectTotalPages
});

const mapDispatchToProps = { getItems };

@connect(mapStateToProps, mapDispatchToProps)
class Example extends Component {
  static propTypes = {
    items: PropTypes.array.isRequired,
    totalPages: PropTypes.number.isRequired
  };
+ @connectInitialProps(mapStateToProps, mapDispatchToProps)
-  static async getInitialProps(ctx) {
+  static async getInitialProps(ctx, props) {

     // We don't need store nor query anyore
-    const { store, query, res } = ctx;
+    const { res } = ctx;

+    const { page, getItems } = props;

     // No need to dispatch manually
-    await store.dispatch(getItems({ page }));
+    await getItems({ page });

     // No need provide store's state manually
-    const { totalPages } = mapStateToProps(store.getState());
+    const { totalPages } = props;

    if (page <= 1 || page > totalPages) {
      res.redirect(301, '/?page=1');
      return {};
    }
  }
  render() {
    const { page, totalPages, items } = this.props;

    return (
      <>
        <h1>
          Page {page} of {totalPages}
        </h1>
        {items.map(item => (
          <Item {...item} />
        ))}
      </>
    );
  }
}