advanced-js-reading-notes

State-with-Reducers

vuseReducer hook

They let you use state and other React features without writing a class.

Accepts a reducer of type (state, action) => newState , and returns the current state paired with a dispatch method.

The benefits to using useDeferredValue is that React will work on the update as soon as other work finishes (instead of waiting for an arbitrary amount of time), and like startTransition , deferred values can suspend without triggering an unexpected fallback for existing content.

useId const id = useId ( ) ; useId is a hook for generating unique IDs that are stable across the server and client, while avoiding hydration mismatches.

For a basic example, pass the id directly to the elements that need it: function Checkbox ( ) { const id = useId ( ) ; return ( < > < label htmlFor = { id } > Do you like React?

As a convenience, we also provide a version of the API with automatic support for memoizing the result of getSnapshot published as use-sync-external-store/with-selector .

read more from here


Ultimate Guide to useReducer

Frontend engineer with rock-solid experience in building complex interactive applications with JavaScript, TypeScript, Vue.js, NuxtJS, React, Next.js, and other tools in the JS ecosystem.

In this tutorial, we’ll explore the useReducer Hook in depth, reviewing the scenarios in which you should and shouldn’t use it.

== 0) { initialCount=+0 } return {count: initialCount}; } // wherever our useReducer is located const [state, dispatch] = useReducer(reducer, initialCount, initFunc); If the value is not 0 already, the initFunc above will reset the initialCount to 0 on page mount, then return the state object.

Basically, it sends the type of action to the reducer function to perform its job, which, of course, is updating the state.

The action to be executed is specified in our reducer function, which in turn, is passed to the useReducer .

Therefore, it’s recommended to use useReducer , which returns a dispatch method that doesn’t change between re-renders, and you can have the manipulation logic in the reducers.

read more from here