@dxos/web-context-react
v0.10.0
Published
React integration with web context protocol
Downloads
2,024
Keywords
Readme
@dxos/web-context-react
React implementation of the Web Component Context Protocol.
Overview
This package allows React components to seamlessly participate in the Web Component Context Protocol, enabling context sharing between React, Web Components, and other frameworks.
Installation
npm install @dxos/web-context-react @dxos/web-contextUsage
Providing Context
Wrap your component tree with ContextProtocolProvider. This handles both React context requests (from descendants in the same tree) and DOM context requests (from Web Components).
import { createContext } from '@dxos/web-context';
import { ContextProtocolProvider } from '@dxos/web-context-react';
const ThemeContext = createContext<{ color: string }>('theme');
const App = () => (
<ContextProtocolProvider context={ThemeContext} value={{ color: 'blue' }}>
<MyComponent />
</ContextProtocolProvider>
);Consuming Context
Use the useWebComponentContext hook to consume context. It first checks for a React provider, and falls back to dispatching a context-request DOM event.
import { useWebComponentContext } from '@dxos/web-context-react';
const MyComponent = () => {
const theme = useWebComponentContext(ThemeContext, { subscribe: true });
return <div style={{ color: theme?.color }}>Hello World</div>;
};