Giter VIP home page Giter VIP logo

react-redux-local's Introduction

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

react-redux-local's People

Contributors

flaviouk avatar

Stargazers

Roman avatar Josh G avatar  avatar Jack Lam avatar Alex Sandiiarov avatar Dmitry Ivakhnenko avatar Matej Bransky avatar  avatar Alireza Valizadeh avatar Asaf Katz avatar Kevin Segal avatar Cyrus Venn Casada avatar Zufrizal Yordan avatar Evgeny Erohin avatar Jelle Versele avatar Anton Korzunov avatar Tyler avatar Geoffrey Dhuyvetters avatar Amit Erandole avatar Wei Zhu avatar Dohyung Ahn avatar Howard H avatar Peter Miles avatar

Watchers

Tyler Krett avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.