3.3.3 • Published 3 years ago

react-gstore v3.3.3

Weekly downloads
1
License
ISC
Repository
github
Last release
3 years ago

React global store (react-gstore)

This library is intended for creating a global store in react

Install

npm i react-gstore

Examples

1 store

import React, { useState } from "react"
import { createContainer } from "react-gstore"
import { render } from "react-dom"
 
function useTheme(initialState = 0) {
    let [theme, setTheme] = useState(initialState)
    let dark = () => setTheme(1)
    let light = () => setTheme(0)
    let anotherTheme = () => setTheme((theme + 1) % 2)
    return { theme, dark, light, anotherTheme}
}
 
let Theme = createContainer(useTheme)
 
function MyBlock() {
    let theme = Theme.useContainer()
    return (
        <div style={{backgroundColor: theme.theme === 0 ? 'white' : '#333333'}}>
            <p>
                Hello world
            </p>
            <button onClick={theme.anotherTheme}>
                Change theme
            </button>
        </div>
    )
}
 
function App() {
    return (
        <Theme.Provider initialState={1}>
            <MyBlock />
        </Theme.Provider>
    )
}
 
render(<App />, document.getElementById("root"))

2 and more stores

import React, { useState } from "react"
import { createContainer, Provider } from "react-gstore"
import { render } from "react-dom"
 
function useTheme(initialState = 0) {
    let [theme, setTheme] = useState(initialState)
    let dark = () => setTheme(1)
    let light = () => setTheme(0)
    let anotherTheme = () => setTheme((theme + 1) % 2)
    return { theme, dark, light, anotherTheme}
}
 
function useCounter(initialState = 0) {
    let [count, setCount] = useState(initialState)
    let decrement = () => setCount(count - 1)
    let increment = () => setCount(count + 1)
    return { count, decrement, increment }
}
 
let Theme = createContainer(useTheme)
let Counter = createContainer(useCounter)
 
function MyBlock() {
    let counter = Counter.useContainer()
    let theme = Theme.useContainer()
    return (
        <div style={{backgroundColor: theme.theme === 0 ? 'white' : '#333333', color: theme.theme === 1 ? 'white' : '#333333'}}>
            <p>
                Hello world
            </p>
            <button onClick={theme.anotherTheme}>
                Change theme
            </button>
            <p>
                Counter
            </p>
            <button onClick={counter.decrement}>-</button>
                <span>{counter.count}</span>
            <button onClick={counter.increment}>+</button>
        </div>
    )
}
 
function App() {
    return (
        <Provider containers={[
            {container: Theme, initialState: 1},
            {container: Counter, initialState: 0}
        ]}>
            <MyBlock />
        </Provider>
    )
}
 
render(<App />, document.getElementById("root"))
3.3.1

3 years ago

3.3.3

3 years ago

3.3.2

3 years ago

3.2.2

3 years ago

3.1.3

3 years ago

3.1.4

3 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.0.6

3 years ago

2.1.8

3 years ago

2.1.7

3 years ago

2.1.6

3 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

1.0.5

4 years ago

2.0.0

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago