woby
Version:
A high-performance framework with fine-grained observable/signal-based reactivity for building rich applications.
57 lines • 1.87 kB
TypeScript
/**
* useContext Hook for Woby Framework
*
* This hook provides access to context values within functional components.
* It works with contexts created by createContext and supports both default and undefined values.
*
* @module useContext
*/
import type { Context, ContextWithDefault } from '../types';
/**
* Accesses the current value of a context
*
* This hook returns the current context value for the specified context.
* It must be called within a functional component or a custom hook.
*
* The hook will first try to get the context value from the nearest Provider component
* above it in the tree. If there is no Provider above it, it will return the defaultValue
* if one was provided to createContext, otherwise it returns undefined.
*
* @template T - The type of the context value
* @param Context - The context object created by createContext
* @returns The current context value
*
* @example
* ```tsx
* // Create a context
* const ThemeContext = createContext('light')
*
* // Use the context in a component
* const ThemedButton = () => {
* const theme = useContext(ThemeContext)
* return <button className={`theme-${theme}`}>Click me</button>
* }
*
* // Provide a value for the context
* const App = () => (
* <ThemeContext.Provider value="dark">
* <ThemedButton />
* </ThemeContext.Provider>
* )
* ```
*
* @example
* ```tsx
* // Context with undefined default
* const UserContext = createContext<User | undefined>(undefined)
*
* const UserProfile = () => {
* const user = useContext(UserContext)
* if (!user) return <div>Please log in</div>
* return <div>Welcome, {user.name}!</div>
* }
* ```
*/
export declare function useContext<T>(Context: ContextWithDefault<T>): T;
export declare function useContext<T>(Context: Context<T>): T | undefined;
//# sourceMappingURL=use_context.d.ts.map