-
-
Notifications
You must be signed in to change notification settings - Fork 4
Usage
souvik edited this page Sep 19, 2020
·
2 revisions
kladi lets you create a global state, and use it in any component in the component tree.
To access the state you need to wrap the <App />
component with <Provider />
from kladi.
import {Provider} from 'kladi'
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
ReactDOM.render(
<Provider>
<App />
</Provider>,
document.getElementById("root")
);
You can also pass inital state object inside provider
<Provider initialState={{
count: 0
}} >
<App />
</Provider
To access and modify the state in any component all you have to do is import useState()
hook from kladi. useState()
takes two parameter
- unique string, you need to give a unique string to the state you want to store inside the global state
- initial value, if you are creating a new global state only then will you need to pass the initial value, accessing a already created variable you do not need to pass initial state.
import {useState} from 'kladi'
import React from 'react'
export default () => {
const [name, setname] = useState('name',"Kladi")
return <>
<h1>{name}</h1>
</>
}