react-global-state-lib

Global state using Context & Hooks APIs

Usage no npm install needed!

<script type="module">
  import reactGlobalStateLib from 'https://cdn.skypack.dev/react-global-state-lib';
</script>

README

react-global-state-lib

package version package downloads standard-readme compliant package license make a pull request JavaScript Style Guide

Global state using Context & Hooks APIs

Table of Contents

Install

This project uses node and npm.

$ npm install react-global-state-lib
$ # OR
$ yarn add react-global-state-lib

Usage

import React, { Component } from 'react'

import initGlobalState from 'react-global-state-lib'


const {GlobalStateProvider, useGlobalState} = initGlobalState({
    count: 1, // initial state
})

function Counter() {
    const [global, setGlobalState] = useGlobalState()

    return <h1>{global.count}</h1>
}

function IncrementBtn() {
    const [global, setGlobalState] = useGlobalState()

    return (
        <button type="button" onClick={() => setGlobalState({count: global.count + 1})}>
            +
        </button>
    )
}

function App() {
    return (
        <GlobalStateProvider>
            <Counter />
            <Counter />
            <Counter />
            <IncrementBtn />
        </GlobalStateProvider>
    )
}

export default App

Contribute

  1. Fork it and create your feature branch: git checkout -b my-new-feature
  2. Commit your changes: git commit -am "Add some feature"
  3. Push to the branch: git push origin my-new-feature
  4. Submit a pull request

License

MIT © tiaanduplessis