Contextism has Typescript support like generics and ... . in createStore you can pass two generics too, first one for the state structure and interface, the second one for the useReducer hook.
type Action = {type: 'increment'} | {type: 'decrement'}type State = { count: number }// The second generic is for useReducer Actionconst context = createStore<State, Action>();// For useState just pass the first generic (State interface generic)const context = createStore<State>();
Contribution
I'm developer, not a perfect person, so I make much mistakes, it means that be free to create issues and then PRs.
Thanks ❤️
Special thanks for contributing and using this project.
// Components you want to use the state there.
<Div />
</Provider>
)
}
// Div.jsx
import { useStateContext, useDispatchContext, useStore } from './store';
const Div = () => {
const state = useStateContext(); // "Value for state"
const dispatch = useDispatchContext(); // dispatch function (setState) in App
// or better one
const [state, dispatch] = useStore();
return ...
}
>
)
}
// App.jsx
import { CountProvider } from './Store';
import Div from './Div';
export function App() {
return (
<CountProvider>
<Div />
</CountProvider>
)
}
// Div.jsx
import { CountStateContext, CountDispatchContext } from './Store';