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

v3.4.0

Published

[![Build Status][build-badge]][build] [![Code Coverage][coverage-badge]][coverage] [![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]

Downloads

54

Readme

react-redux-local

Build Status Code Coverage version downloads

PRs Welcome MIT License

Watch on GitHub Star on GitHub Tweet

The problem

I love redux, but building a small and simple local reducer component on every project is not on top of the list of things I like to do the most, plus what if I want to take advantage of sagas, dev tools and the new context api? It becomes a not so simple component very quickly.

The solution

You can think of react-redux-local as a mini, yet powerful version of react-redux, the api is very simple, abstracting away things like creating a redux store, adding middleware, binding actions and plugging in the redux dev tools.

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

yarn add react-redux-local

Usage

LocalReducer

import LocalReducer from 'react-redux-local'

// https://github.com/erikras/ducks-modular-redux
import { actions, reducer, saga, middleware, devToolsOptions } from './duck'

const App = () => (
  <LocalReducer
    actions={actions}
    reducer={reducer}
    saga={saga}
    middleware={middleware}
    devToolsOptions={devToolsOptions}
  >
    {(state, actions, dispatch) => (
      <YourComponent state={state} actions={actions} />
    )}
  </LocalReducer>
)

createContext

import { createContext } from 'react-redux-local'

import { actions, reducer, saga, middleware, devToolsOptions } from './redux'

const { Provider, Consumer } = createContext({
  actions,
  reducer,
  saga,
  middleware,
  devToolsOptions,
})

const Up = () => (
  <Consumer mapActions={({ countUp }) => countUp}>
    {(_, action) => <button onClick={action}>UP</button>}
  </Consumer>
)

const Down = () => (
  <Consumer mapActions={({ countDown }) => countDown}>
    {(_, action) => <button onClick={action}>DOWN</button>}
  </Consumer>
)

// Will only rerender when the "counter" state changes
const Count = () => (
  <Consumer mapState={({ counter }) => counter}>
    {state => <h3>Count: {state}</h3>}
  </Consumer>
)

// Will only rerender when the "total" state changes
const TotalCount = () => (
  <Consumer mapState={({ total }) => total}>
    {state => <h3>Total count: {state}</h3>}
  </Consumer>
)

// Will only rerender when the "downs" state changes
const DownsOnly = () => (
  <Consumer mapState={({ downs }) => downs}>
    {state => <h3>Downs: {state}</h3>}
  </Consumer>
)

const App = () => (
  <Provider>
    <Up />

    <Down />

    <Count />

    <TotalCount />

    <DownOnly />
  </Provider>
)

Api

Props

Tip: createContext takes the same props as LocalReducer

reducer

func.isRequired

A reducer specifies how the application's state changes in response to actions sent to the store.

Learn More

e.g.

const initialState = { counter: 0, total: 0, downs: 0 }
const reducer = (state = initialState, action) => {
  switch (action.type) {
    case 'COUNT_UP':
      return {
        counter: state.counter + 1,
        total: state.total + 1,
        downs: state.downs,
      }

    case 'COUNT_DOWN':
      return {
        counter: state.counter - 1,
        total: state.total + 1,
        downs: state.downs + 1,
      }

    default:
      return state
  }
}

actions

objectOf(func.isRequired).isRequired

Actions are payloads of information that send data from your application to your store. They are the only source of information for the store.

Learn More

e.g.

const actions = {
  countUp: () => ({ type: 'COUNT_UP' }),
  countDown: () => ({ type: 'COUNT_DOWN' }),
}

saga

func

Aims to make application side effects (i.e. asynchronous things like data fetching and impure things like accessing the browser cache) easier to manage, more efficient to execute, simple to test, and better at handling failures.

Learn More

e.g.

import { put } from 'redux-saga'

function* doubleCount() {
  put(actions.countUp())
}

function* saga() {
  yield takeEvery('COUNT_UP', doubleCount)
}

middleware

arrayOf(func.isRequired)

It provides a third-party extension point between dispatching an action, and the moment it reaches the reducer.

Learn More

const middleware = store => next => action => {
  console.log(action.type)
  return next(action)
}

devToolsOptions

object

Allows for a better development experience with redux.

Dev Tools

Learn More

e.g.

const devToolsOptions = { name: 'My own devtools tab' }

children

func.isRequired

The term “render prop” refers to a simple technique for sharing code between React components using a prop whose value is a function.

Learn More

Video: Michael Jackson - Never Write Another HoC

Michael Jackson - Never Write Another HoC

<Consumer /> props (from createContext)

mapState

func | state => undefined

Behaves like mapStateToProps from react-redux with the exception that it won't be available in the props (duh) and you are not required to return an object (thank you render props)

mapActions

func | (actions, dispatch) => undefined

Allows you to pick what actions you want available in the second argument of your render function. dispatch is very much optional since all the actions are binded automatically.

<LocalReducer /> render function

;(state, actions, dispatch) => <YourComponent />

state

Your application state.

actions

Binded actions. (You don't need to dispatch)

dispatch

Optional function that allows you to dispatch other actions.

dispatch({ type: 'VERY_CUSTOM_ACTION' })

Examples

Other Solutions

local-react-redux

local-react-redux-saga

react-local-reducer

react-copy-write

LICENSE

MIT