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-cornell

v2.0.0

Published

Write less, do more with Redux

Downloads

8

Readme

Redux Cornell Build Status Coverage Status

Dedicated to Chris Cornell R.I.P

A Redux library which lets you remove most of the boilerplate associated with writing a Redux application, yet allows you to customize it completely without loosing control.

How does it work?

The library receives an initial state and generates actions, selectors and a super reducer which does all the heavy lifting for you so you can focus on the most important thing - your app.

Installation

npm install --save redux-cornell

Initializing the reduxCornell

Before creating your root reducer, initialize reduxCornell with your initial state:

// `src/redux/reducers/index.js`

// import ReduxCornell
import reduxCornell from 'redux-cornell';
import { combineReducers } from 'redux';

// The function expects an object with the `initialState` key.
// In this example 'episodes' and 'showInfo' will become the "reducers".
const { selectors, actions, superReducer } = reduxCornell({
  initialState: {
    episodes: {
      loaded: false,
      data: [],
      expanded: {}
    },
    showInfo: {
      visible: false
    }
  }
});

// You will get default actions and selectors (covered below).
// Export them so they are available across your app.
export { selectors, actions };

// Add the superReducer to your rootReducer`
const rootReducer = combineReducers({
  superReducer: reduxCornell.superReducer
});

// Export the rootReducer like you usually do.
export default rootReducer;

Selectors

The selectors are automatically generated from the initialState. They are built in the following format:

get<Reducer><Property>

The selectors accept only one parameter - the state.

So from the initialState in our example, we will get the following selectors:

getEpisodesLoaded
getEpisodesData
getEpisodesExpanded
getShowInfoVisible

Then, in your mapStateToProps you can use them like this:

import { selectors } from '../redux/reducers';

const mapStateToProps = (state) => ({
  episodes: selectors.getEpisodesData(state)
});

Actions

The actions are automatically generated from the initialState. Redux Cornell will analyze your initialState and generate actions according to the property's initial value.

Boolean

If the property's initial value is a boolean you will get an action in the following format: toggle<Reducer><Property>

So in our example, you will get:

toggleEpisodesLoaded
toggleShowInfoVisible

These actions don't receive any parameters as they toggle the boolean back and forth.

Array

If the property's initial value is an array, you will get an action in the following format: concat<Reducer><Property>

So in our example, you will get:

concatEpisodesData

These actions accept an array as a parameter and will concat it to the state.

Object

If the property's initial value is an object, you will get an action in the following format: extend<Reducer><Property>

So in our example, you will get:

extendEpisodesExpanded

These actions accept an object as a parameter and will extend the state with it.

Set & Nullify Actions

In addition to the actions mentioned above, each property will also generate an action for setting and nullifying the property's value in case you need to overwrite things. They will be in the following format: set<Reducer><Property> and nullify<Reducer><Property>

So in our example, you will get:

setEpisodesLoaded
nullifyEpisodesLoaded
setEpisodesData
nullifyEpisodesData
setEpisodesExpanded
nullifyEpisodesExpanded
setShowInfoVisible
nullifyShowInfoVisible

The set actions receive one parameter - the value which you want to set for that property. The nullify actions don't receive any value.

Override Action

As a last resort, you can always override the entire value of a reducer using the override action. The override action is generated in the following format: override<Reducer>.

The action takes an object as the parameter.

So in our example, you will get

overrideEpisodes

How to use the actions

In your app you can use the action as you would with "regular" Redux action:

// Map directly if you are using thunk
import { actions } from '../redux/reducers';
const { extendEpisodesExpanded } = actions;

...

export default connect(mapStateToProps, { extendEpisodesExpanded })(EpisodesContainer);


// OR using bindActionCreators
import { bindActionCreators } from 'redux'

const mapDispatchToProps = (dispatch) => ({
  extendEpisodesExpanded: bindActionCreators(extendEpisodesExpanded, dispatch)
});

export default connect(mapStateToProps, mapDispatchToProps)(EpisodesContainer);

// OR us the dispatch as a prop in your component

onClick() {
  this.props.dispatch(extendEpisodesExpanded({ something: true }));
}

If you need to do something like fetching data from the server, you can fetch the data and then call one of the actions which were generated. Here is an example of using Redux Thunk to fetch a show and then it's episodes and finally dispatch the concatEpisodesData action with the data when everything is complete:

// container
import { fetchEpisodes } from '../actions/episodeFetcher';

export default connect(mapStateToProps, { fetchEpisodes })(EpisodesContainer);


// episodesFetcher
import * as imdb from 'imdb-api';
import { actions } from '../redux/reducers';

const showId = 'tt4574334'; // the "Stranger Things" show id on imdb
const apiKey = 'your_omdb_token'; // an OMDB token

export const fetchEpisodes = () => (dispatch) => {
  imdb.getById(showId, { apiKey }).then(results => {
    results.episodes().then(episodes => {
      dispatch(actions.concatEpisodesData(episodes));
    })
  });
}

Reducers

None needed! The super reducer takes care of everything for you! 😎

Full Working Example

In the example directory you have a fully working example which fetches the "Stranger Things" episode list from imdb. You can also toggle between the episode list or show's info and you can expand/collapse each episode to see more information. The example covers the toggle, concat, extend, set and nullify actions.

In the episodesActions.js file, add your OMDB token. You can generate one here: https://www.omdbapi.com/apikey.aspx

To run:

cd example
npm install
npm start

You can see the app online here: https://eyaleizenberg.github.io/imdb_list/index.html

Feedback

Found a bug? Have a suggestion? Open an issue on Github. Like it? Show some love on Twitter @EyalEizenberg and @WixEng ❤️