shadcn-ahooks

useGetState

Add a getter method to the return value of React.useState to get the latest value

Overview

Add a getter method to the return value of React.useState to get the latest value

Documentation and Examples

Installation

Open in
pnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useGetState.json
npx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useGetState.json
yarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useGetState.json
bun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useGetState.json

Add a getter method to the return value of React.useState to get the latest value

Examples

Default usage

TypeScript definition

import { Dispatch, SetStateAction } from 'react';
type GetStateAction<S> = () => S;

function useGetState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>, GetStateAction<S>];
function useGetState<S = undefined>(): [S | undefined, Dispatch<SetStateAction<S | undefined>>, GetStateAction<S | undefined>];

API

const [state, setState, getState] = useGetState<S>(initialState)

On this page