@etsoo/react
Version:
TypeScript ReactJs UI Independent Framework
42 lines (41 loc) • 1.24 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import React from "react";
/**
* State
*/
export class State {
/**
* Generic to create state context and provider
* @param reducer Reduce function
* @param initState Init state
* @param uiCreator Additional UI creator
*/
static create(reducer, initState, calls, uiCreator) {
// State context
const context = React.createContext(calls);
// State context provider
const provider = (props) => {
// Destruct
const { children, update } = props;
// Update reducer
const [state, dispatch] = React.useReducer(reducer, initState);
// Callback
if (update != null)
update(dispatch);
if (uiCreator) {
// Custom renderer
return uiCreator(state, dispatch, props);
}
else {
// Context new value
const value = { ...calls, state, dispatch };
return _jsx(context.Provider, { value: value, children: children });
}
};
// Return
return {
context,
provider
};
}
}