Global state using Context & Hooks APIs
This project uses node and npm.
$ npm install react-global-state-lib
$ # OR
$ yarn add react-global-state-lib
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
- Fork it and create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am "Add some feature"
- Push to the branch:
git push origin my-new-feature
- Submit a pull request
MIT © tiaanduplessis