Giter VIP home page Giter VIP logo

reduceless's Introduction

Reduceless

Simple abstraction over Redux to make state management as easy as React's vanilla setState() but with advantages of Redux.

The idea is easy: you connect component to a slice of your Redux state to give this component full control over this part of the state. It can read from this state and write to it in React style with setState and replaceState. You don't need any custom reducers, actions or constants to do it. Just read state and write to it.

Don't worry you are still able to use Redux in the normal way. It's just set of helper to avoid boilerplate for simple actions.

Installation

npm install reduceless --save

How to use:

1.wrap your root reducer with wrapReducerWithSetGlobalState

import {wrapReducerWithSetGlobalState} from 'reduceless';

const reducer = wrapReducerWithSetGlobalState(
  // Your normal reducers are going here
  // combineReducers({
  //   reducer1,
  //   reducer2,
  // })
);

2.connect your component to a slice of your state

import {connectSlicedState} from 'reduceless';

@connectSlicedState('pages.blogs.data.activePost')
const PostForm = ({state, setState, replaceState}) => {
  return (
    <div>
      <input
        type="checkbox"
        checked={state.checked}
        onChange={e => setState({'checked': !state.checked})}/>
      <br/>
      value: {state.checked.toString()}
    </div>
  )
}

PostForm component will recieve part of your Redux state located at pages.blogs.data.activePost in state prop. Component can change it using setState(newState) and replaceState(newState) props. This path could even not exist. It will be created automatically when component will write to it.

3.[Optional] Use can use replaceStateByPath and setStateByPath action creators for advanced scenarios.

import {connectSlicedState} from 'reduceless';
import _ from 'lodash';

const Form = ({checked, setChecked}) => {
  return (
    <div>
      <input
        type="checkbox"
        checked={checked}
        onChange={e => setChecked({'checked': !state.checked})}/>
      <br/>
      value: {checked.toString()}
    </div>
  )
}

export default connect(
  (state, props) => ({
    checked: _.get(state, 'pages.blogs.data.activePost'),
  }),
  dispatch => ({
    setChecked: checked => dispatch(replaceStateByPath('pages.blogs.data.activePost', checked)),
  })
)(Form);

Still no actions, reducers or constants.

4.[Optional] use initialReducer // TODO

Basic API

###wrapReducerWithSetGlobalState(reducer) Wraps your reducer(probably at root level but you can use it in any level of your reducers tree) with another reducer that catches events sent by reduceless(SET_STATE_BY_PATH, REPLACE_STATE_BY_PATH).

###connectSlicedState(path)(component) Connects your component to a slice of your redux state located at path. Sends following props to the component:

  1. state - slice of the state located at path. It doesn't matter what exaclty is stored there. It could be object, array, simple value. Actually there could be no such path in your state at all. In that case you will get state === undefined so you can use defaultProps to populate this prop.

  2. setState(newState) – action (already wrapped in dispatch) that will merge slice of the state located at path with newState. If this path does not exist in your redux state it will be created. Calls setStateByPath under the hood.

  3. replaceState(newState) – action (already wrapped in dispatch) that will replace slice of the state located at path with newState. If this path is does not exist in your redux state it will be created. Calls replaceStateByPath under the hood.

Advanced API

###setStateByPath(path, newState) Action creator that merges state located by path with newState.

dispatch(setStateByPath('posts.3.data', {title: 'new title'}))

replaceStateByPath(path, newState)

Action creator that merges state located by path with newState.

dispatch(replaceStateByPath('posts.3.data.title', 'new title'))

reduceless's People

Contributors

nosovsh avatar anorudes avatar andrepolischuk avatar

Stargazers

Guilherme Bayer avatar Hervis Pichardo | designer and web developer avatar David Gaitsgory avatar Marcos Leonel avatar  avatar Kristi Buda avatar Yury Shulaev avatar Nathan Harper avatar Jonathan Pettersson avatar Mike Piccolo avatar Alexander avatar  avatar  avatar Nick Sweeting avatar Cem Turan avatar Anton Burnashev avatar Honggui avatar Denis Izmaylov avatar Vicente de Alencar avatar Felipe Leusin avatar Matt avatar Ivan Babak avatar Brad Pillow avatar Valery Bugakov avatar Andrey avatar  avatar Nikita Gusakov avatar Loginov Roman avatar Cloudo avatar Vadim Chernysh avatar Denis Denisov avatar Philipp Andreychev avatar Mikey avatar Igor Suvorov avatar Aleh Kashnikaў avatar Charles Pick avatar Dmitriy Lazarev avatar Vladimir Starkov avatar Sergey Smyshlyaev avatar Anton Petrov avatar Dmitry Belyaev avatar Roman Krivtsov avatar

Watchers

 avatar James Cloos avatar Loginov Roman avatar

Forkers

anorudes

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.