1.0.1 • Published 2 years ago

rsoftvn-state v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

USE GLOBAL STATE

Usage

  1. Tạo 1 file mới, đặt tên bất kỳ
  2. Khởi tạo các global state
import { createStates } from "rsoftvn-state"

export let useGlobalState = createStates({
    example: "EXAMPLE VALUE",
    exampleUserData: {
        username: "Tung Hwang",
        age: 23
    }
})
  1. Sử dụng global state và set global state
import { useGlobalState } from "./exampleFile"

function Example() {

    // useGlobalState luôn nhận vào 1 string tương ứng với tên của state đã tạo
    const [exampleUserData, setExampleUserData] = useGlobalState("exampleUserData")
    const [example, setExample] = useGlobalState("example")

    return <div>

        <h3>{ exampleUserData.age }</h3>
        <h3>{ example }</h3>

        <button 
            onClick={
                ()=>setExampleUserData(u => {
                    return { ...u, age: 24 }
                })
            }>
            + Age
        </button>

        <button onClick={()=>setExample(e => "NEW VALUE")}>
            Change
        </button>
    </div>
}