1.0.1 • Published 2 years ago
rsoftvn-state v1.0.1
USE GLOBAL STATE
Usage
- Tạo 1 file mới, đặt tên bất kỳ
- 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
}
})
- 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>
}