UNPKG

react-server-only-context

Version:

A solution for enabling context sharing in React Server Components, providing an alternative to React.createContext.

20 lines (19 loc) 499 B
import * as React from 'react'; export function createServerOnlyContext(defaultValue) { const value = React.cache(() => ({ current: defaultValue, })); function Provider(props) { value().current = props.value; return props.children; } const context = { Provider, _currentValue: value, }; return context; } export function readContext(context) { const _currentValue = context._currentValue(); return _currentValue.current; }