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

redux-context

v0.2.1

Published

A redux delegate object used to create store, render page, and integrate React-Router and more

Downloads

8

Readme

redux-context

A redux delegate object used to create store, render page, and integrate React-Router and more.

Travis NPM version Downloads

Motivate

It's really popular to integrate React with React-Router, Redux and related development tools.

Most likely, we'll need install the following packages:

  yarn add react
  yarn add react-dom
  yarn add react-redux
  yarn add react-router
  yarn add react-router-dom
  yarn add react-router-redux
  yarn add redux
  yarn add redux-thunk
  yarn add -D react-hot-loader
  yarn add -D redux-devtools
  yarn add -D redux-devtools-dock-monitor
  yarn add -D redux-devtools-log-monitor
  yarn add -D redux-logger

And do a lot of work to integrate them well. It's so boring and complicated for me, especially there are several React projects need to be maintained.

This package extracts these works from my project, and provides a better API to accomplish all related works.

Installation

In your React app, install the following packages:

  yarn add react
  yarn add react-dom
  yarn add redux-context
  yarn add -D react-hot-loader
  yarn add -D redux-devtools-extension
  yarn add -D redux-logger

Why redux-devtools-extension?

redux-devtools-extension is built on the top of redux-devtools, and integrates several useful tools.

Why redux-logger?

redux-logger will print the information of dispatched action of Redux. You may remove it if you don't like it.

User Guide

In entry JavaScript, we could use redux-context to create history which is used by React-Router, initial Redux store, and render React component to DOM element.

  import React from 'react';
  import ReduxContext from 'redux-context';
  import MyApp from './MyApp';
  import reducers from './reducers';

  let domElement = document.getElementById('page');
  let context = ReduxContext
    .forDOM(domElement)
    .configureStore(reducers)
    .render(MyApp);

  if (module.hot) {
    module.hot.accept('./reducers', () => {
      context.replaceReducer(require('./reducers').default);
    });
    module.hot.accept('./MyApp', () => {
      context.render(MyApp);
    });
  }

API References

forDOM(domElement)

  • If domElement is a DOM Element object, it will be used directly.
  • If domElement is String object, it will be used to find the Element via document.querySelector().
  • If domElement is omitted, redux-context will try to find DOM Element which id is page or root.

If a valid domElement is existed, redux-context will extract data-* attributes from it, and merge them with window.pageProps.

  let defaultProps = Object.assign({}, dataAttrs, window.pageProps)

The defaultProps will be send to ReactElement which is used in render() as props.

createHistory(creator)

  • creator is a callback function, used to create history object. It will receive defaultProps as argument.

In fact, there is no need to call createHistory() manually. redux-context will create a browserHistory for you as default.

configureStore(reducers, initialState = undefined)

  • reducers is a plain object, which contains several reducer functions
  • initialState is optional

render(ReactElement, props = undefined)

  • ReactElement is a class or instance of React Component
  • props will be merged with defaultProps, and inject store and history

replaceReducer(reducers)

  • reducers is a plain object, which contains several reducer functions

React Component tree

As result, redux-context will generate different following React Component trees for production mode and development mode.

Production mode

  <withRedux>
    <Provider>
      <ConnectedRouter>
        <Router>
          <MyApp />
        </Router>
      </ConnectedRouter>
    </Provider>
  </withRedux>

Development mode

  <AppContainer>
    <withRedux>
      <Provider>
        <ConnectedRouter>
          <Router>
            <MyApp />
          </Router>
        </ConnectedRouter>
      </Provider>
    </withRedux>
  </AppContainer>

Redux DevTools (and Extensions)

For more information, you may check the page of redux-devtools-extension.