Use current state

Get the latest value in functional component. By default, state in a React FC can only get the value at render time, not the latest.

This is a small tool to get the latest value, It is very useful in async actions such as setTimeout. if you like a bigger solution, try useReducer.

This is custom React Hooks, you need to follow the Basic Rules when you use it.

Standard

APIs / Schnittstellendokumentation

useCurrentState

type CurrentStateType<S> = [
  S,
  Dispatch<SetStateAction<S>>,
  MutableRefObject<S>,
]

const useCurrentState<S> = (
  initialState: S | () => S,
) => CurrentStateType<S>

Co-Creators