1.3.1 • Published 28 days ago

@heliobentes/global-state v1.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
28 days ago

Status GitHub Issues GitHub Pull Requests License


Start here

About

Reducers... Actions... Dispatcher... Providers... Why so complicated? Manage your App-Wide States with a single line of code. 100% compatible with default React useState().

Getting Started

Prerequisites

Global State requires only React and is a React Hook component only. It won't work for classes (you are more than welcome to modify it and make it work with classes! Thanks in advance!)

Installing

Install it using npm

npm i @heliobentes/globalstate

Usage

Use Global State as you would use the default React useState() hook (learn more) and simply add an identifier to it on every component you want to manage the state on.

Regular useState

const [loading, setLoading] = useState(false);

Normal usage

//add this line to every component you want to manage the state on
const [loading, setLoading] = useGlobalState("page-loader", false); 

Forcing a state reset

Your state will assume value for the first time the hook is called in your application. Every subsequent render will ignore the value unless you specify forceNewState as true. This will force your global state to assume the new value passed.

//this will forcec the globalstate to the specified value
const [loading, setLoading] = useGlobalState("page-loader", false, true);

Example

import React from 'react';
import useGlobalState from "@heliobentes/global-state";

function Example() {
  // Declare a new state variable, which we'll call "count"
  const [count, setCount] = useGlobalState("count",0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

Authors

1.3.1

28 days ago

1.3.0

28 days ago

2.0.0

30 days ago

1.2.0

2 months ago

1.1.2

2 months ago

1.1.1

3 months ago

1.1.0

3 months ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago