Skip to content

Ulmanb/react-state-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

react-state-wrapper

Are you tired of this.setState() everywhere? this one is for you!

what

Extremely simple and tiny utility with no external dependencies to never write this.setState again.

usage

Import the constructor function:

import Wrapper from 'rootToDependency';

In the react component whose state you want to wrap constructor's create the wrapper using the imported function and sending this as a parameter:

constructor(props) {
  super(props);

  // Initialize state
  this.state = {
    loggedIn: null
  };

  // Create the state wrapper
  this.stateWrapper = new Wrapper(this);
}

Then you can just replace any access to state with the new wrapper object. Getting state data:

let isLoggedIn = this.stateWrapper.loggedIn;

Setting state data:

this.stateWrapper.loggedIn = false;

And thats it.

The state wrapper will call this.setState({ loggedIn: false }) for you.

Enjoy 😎

About

A state wrapper for react.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published