README
ajayda24-store
Easy state management for react
Installation
npm i ajayda24-store
Usage
import globalState,{useStore,useInitStore} from 'ajayda24-store'
In App.js
Call useInitStore()
Save to Global State
useStore(identifierString,value)
Eg: useStore('formState',state)
Access globalState
console.log(globalState)
Example
App.js
import React, { useState } from 'react'
import globalState, { useStore, useInitStore } from 'ajayda24-store'
import Form from './Form'
const App = (props) => {
useInitStore() //To Activate globalState
// Making a simple state using useState
const [state, setState] = useState({
a: 'hello',
b: ['hi', 'hello'],
c: { ha: 'qwe', hlo: 'hehe' },
})
useStore('state', state) //Saving state to globalState
console.log(globalState)
return (
<div>
<Form />
{globalState.form ? globalState.form.name : null} {/* Tom */}
</div>
)
}
export default App
Form.js
import React,{useState} from 'react'
import globalState, { useStore } from 'ajayda24-store'
const Form = (props) => {
// Making a simple state using useState
const [name, setName] = useState('Tom')
useStore('name', name) //Saving state to globalState
console.log(globalState)
// {
// app: {
// state:{
// a: 'hello',
// b: ['hi', 'hello'],
// c: { ha: 'qwe', hlo: 'hehe' },
// }
// },
// form:{
// name: 'Tom'
// }
// }
return (
<div>
{globalState.app.state.a} {/* hello */}
</div>
)
}
export default Form