A lightweight, type-safe React context library that simplifies state management in React applications.
- 🚀 Lightweight and performant
- 🔒 Type-safe with TypeScript
- 🎯 Simple and intuitive API
- 📦 Zero dependencies
- 🧪 Fully tested
- 📚 Storybook documentation
npm install react-ditto-context
# or
yarn add react-ditto-context
# or
bun add react-ditto-context
import { DittoContextProvider, useDittoContext } from 'react-ditto-context';
// Define your context type
type MyContext = {
count: number;
increment: () => void;
};
interface ContextState {
message: string
}
const context = React.createContext<ContextState | null>(null)
// Create your context provider
const MyComponent = ({ children }: { children: React.ReactNode }) => {
return (
<DittoContextProvider context={context} value={{
message: 'data'
}}>
{children}
</DittoContextProvider>
);
};
// Use the context in your components
const Reader = () => {
const { message } = useDittoContext<MyContext>();
return (
<p>{message}</p>
);
};
A type-safe context provider component.
<DittoContextProvider<T> value={value}>
{children}
</DittoContextProvider>
-
value: T
- The context value -
children: React.ReactNode
- Child components
A hook to access the context value.
const value = useDittoContext<T>();
# Install dependencies
bun install
# Run tests
bun test
# Start Storybook
bun run storybook
# Build the library
bun run build
Contributions are welcome! Please feel free to submit a Pull Request.
MIT © Roberto Ríos