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

react-redux-ready

v1.0.3

Published

Simpler way to use redux with react

Downloads

3

Readme

React Redux Ready

React-Redux-Ready provides a simpler way to use Redux in React powered apps.

Installation

npm install --save react-redux-ready

How to Use

Application entry file

You need to do two things here:

  1. Import the store from 'react-redux-ready' then pass it to the Provider component
  2. Call updateRootReducer right before calling the render method
import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { store, updateRootReducer } from './react-redux-ready';
import Main from './Main';

const App = (
  <Provider store={store}>
    <Main />
  </Provider>
);

// you must call updateRootReducer before rendering your app
updateRootReducer();

// render your app
render(App, document.getElementById('app'));

Component file

Here is what you need to do in a component file:

  1. Import the connect function from 'react-redux-ready'
  2. Import the reducer and all action creators related to this component
  3. Pass them all to the connect function and export the returned component
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from './react-redux-ready';
import reducer from './component-reducer';
import * as actions from './component-actions';

const MainPage = props => (
  <div>
    <button onClick={() => console.log(props.myStateKey)}>
      I will console.log the props object when I am clicked
    </button>
    
    <button onClick={() => console.log(props.actions.myActionsKey)}>
      I will console.log the actions object when I am clicked
    </button>
  </div>
);

MainPage.propTypes = {
  myStateKey: PropTypes.object.isRequired,
  actions: PropTypes.object.isRequired,
};

export default connect(MainPage, { reducer, actions, stateKey: 'myStateKey', actionsKey: 'myActionsKey' });

Component state can then be accessed via this.props.myStateKey and component actions can be accessed via this.props.actions.myActionsKey, this is to avoid naming conflicts between different state keys, actions and component defined properties.

Object destructuring

You can use object destructuring to extract state and actions and avoid writing long lines of code.

For a component with the following configuration:

export default connect(SignupPage, { reducer, actions, stateKey: 'signup', actionsKey: 'signup' });

You would do the following:

render() {
  const { username, email } = this.props.signup;
  const { login, resetPassword } = this.props.actions.signup;
    
  return (
    <div>
      <h1>
      	Your username: {username}
        Your e-mail: {email}
      </h1>
      
      <button onClick={login}>
        Login
      </button>

      <button onClick={resetPassword}>
        Forgot your password?
      </button>
    </div>
  );
}

The connect method parameters

component (required)

React component reference

configObject (optional)

Configuration object that contains the following keys:

  • reducer: Reference to the component reducer.
  • actions: Reference to the actions object.
  • stateKey: Namespace that will be used to pass component state as props.
  • actionsKey: Namespace that will be used to pass component actions as props.

Middlewares

To use a middleware, import useMiddleware method and pass it the middleware, here is an example using React Router (v3.0.5):

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { store, updateRootReducer, registerReducer, useMiddleware } from 'react-redux-ready';
import { IndexRoute, Route, Router, browserHistory } from 'react-router';
import { syncHistoryWithStore, routerMiddleware, routerReducer } from 'react-router-redux';
import Main from './Main';

// register router reducer
registerReducer('routing', routerReducer);

// create the routing middleware
useMiddleware(routerMiddleware(browserHistory));

// create history
const history = syncHistoryWithStore(browserHistory, store);

const MainApp = (
  <Provider store={store}>
    <Router history={history} key={Math.random()}>
      <Route path="/" component={Main}>
        ...
      </Route>
    </Router>
  </Provider>
);

updateRootReducer();

// render everything
render(MainApp, document.getElementById('app'));