GithubHelp home page GithubHelp logo

aganglada / with-reducer Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 1.0 199 KB

🔨 React HoC setState with reducer

Home Page: https://www.npmjs.com/package/with-reducer

JavaScript 86.29% HTML 13.71%
react component high-order-component hoc redux reducer setstate

with-reducer's Introduction

with-reducer

React High order Component that allow you to dispatch actions with no need of redux as dependency.

Redux architecture is really nice, but you might not need it, instead you can only use setState.

But what about going a little bit further and keep your reducers as the key part of your application. with-reducer allows you to perfom this setState call, with a known interface like redux.

Install

npm install with-reducer
yarn add with-reducer

Usage

  1. Create a component and export it with withReducer. You will need to provide a valid reducer for that component to work.

By doing that, you will get just for free a dispatch function into your props.

// example.js

import React, { Component } from 'react';
import withReducer from 'with-reducer';
import reducer from './example.reducer';
import { INCREMENT, DECREMENT } from './example.actions';

class Example extends Component {
  state = {
    counter: 0
  };

  increment = () => {
    this.props.dispatch(INCREMENT);
  };

  decrement = () => {
    this.props.dispatch(DECREMENT);
  };

  render() {
    const { counter } = this.props;

    return (
      <div>
        <button onClick={this.decrement} type="button">
          -
        </button>
        <span>{counter}</span>
        <button onClick={this.increment} type="button">
          +
        </button>
      </div>
    );
  }
}

export default withReducer(reducer)(Example);
  1. Create your reducer
// example.reducer.js

import { INCREMENT, DECREMENT } from './example.actions';

export default (state, action) => {
  switch (action.type) {
    case INCREMENT:
      return Object.assign({}, state, {
        counter: state.counter + 1
      });
    case DECREMENT:
      return Object.assign({}, state, {
        counter: state.counter - 1
      });
    default:
      return state;
  }
};
  1. And then your actions
// example.actions.js

export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';

That's it. Now you can do all your state modifications using dispath like you were dispathing a function in your redux app.

dispatch(actionType, payload = {})

  • actionType: string
  • payload: object, data you might need for that action.

This function will receive the name of the action to be executed and the payload (in case you need it).

It will automatically set the state and pass the same values as props in your component.

Contributing

I would love to see you contributing to with-reducer, also by giving feedback. If you think something is missing, create a new issue.

Pull request are more than welcome ❤️️

License

MIT © aganglada

with-reducer's People

Contributors

aganglada avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.