UNPKG

redux-toolkit-state

Version:

🚀 A powerful & lightweight React hook library that simplifies Redux state management with a familiar useState-like API. Built on Redux Toolkit for optimal performance.

43 lines (42 loc) • 1.34 kB
import { useSelector } from 'react-redux'; /** * Hook for selecting specific parts of global state * @param key - The global state key * @param selector - Function to select specific data from the state * @returns The selected value */ export function useReduxStateSelector(key, selector) { return useSelector((state) => { const globalState = state[key]; if (globalState === undefined) { throw new Error(`Global state with key "${key}" does not exist.`); } return selector(globalState); }); } /** * This hook is useful for accessing a specific value from the global state without needing to provide a selector function. * Hook for selecting a specific global state value * * @param key - The global state key * @template T - The type of the global state value * * @returns The value of the global state */ export const useReduxStateValue = (key) => { return useSelector((state) => state[key]); }; /** * Hook for selecting multiple global states at once * @param keys - Array of global state keys * @returns Object with selected states */ export function useMultipleGlobalStates(keys) { return useSelector((state) => { const result = {}; keys.forEach((key) => { result[key] = state[key]; }); return result; }); }