UNPKG

dreamstate

Version:

Store management library based on react context and observers

40 lines (37 loc) 1.45 kB
import { useState, createElement } from 'react'; import { createScope } from './createScope.js'; import { ScopeContext } from './ScopeContext.js'; /** * Lazy initializer of current scope context provider props object that preserves object references. * Composes props object that will be same for each tree re-render. * * @returns {ProviderProps<IScopeContext>} The initialized props object for the scope provider. */ function scopeStateInitializer() { return { value: createScope() }; } /** * Provides an isolated scope for signaling and context managers. * * The `ScopeProvider` component wraps its children within a dedicated scope, ensuring that signals * and context managers operate independently from other parts of the React tree. This isolation * helps prevent interference between different parts of the application and maintains the integrity * of context data and signal handling. * * @param {IScopeProviderProps} props - The properties for the scope provider, including the children * to be rendered within the isolated scope. * @returns {ReactElement} A React element representing the scope provider. */ function ScopeProvider(props) { var scopeState = useState(scopeStateInitializer); return createElement(ScopeContext.Provider, scopeState[0], props.children); } /* * Easier devtools usage for dev environment. */ { ScopeProvider.displayName = "Dreamstate.ScopeProvider"; } export { ScopeProvider };