...

/

Efficiently Managing Current Values Using the useCurrent Hook

Efficiently Managing Current Values Using the useCurrent Hook

Learn about the useCurrent custom hook that overcomes lag issues associated with React applications.

Introduction to the useCurrent hook

In the internal implementation of the useCurrent custom hook, the React useState hook is utilized. The useCurrent hook is a custom React hook that can be used to maintain a reference to the current value of a changing variable or state within a functional component.

When using the useState, we encountered quite a few issues that prevented a newcomer from understanding how to use it properly, mainly from the inherited laggy behavior due to the fact that the state value does not change right after the dispatch.

const [state, dispatchState] = useState(0)

In the preceding line, if we understand the dispatchState function is to dispatch and request a change, then there's not much we need to do because that's how React designs the useState. However, most often, we tend to think differently: ... ...