You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why JS is eating HTML - Summary: React provides a declarative API that helps assure invalid (out of sync) conditions can't occur. Without React, it's your job to keep the HTML and JS data in sync.
Why is React great? Because it changed how we think about applications. We now write apps by defining: “when the data looks like A, the app should look like B”, and “when the user does X, change the data like Y”. We no longer have to write brittle code like: “when the user does X, update these DOM elements to look like B”. In jQuery, we had to manually keep the DOM and state and in sync.
Code in curly braces is left alone. (interpolation). This allows you to dynamically inject variables into props and children. Interpolation must be JavaScript expressions because they're essentially the right hand of an object assignment or used as an argument to a function call.
It's async. setState calls are batched. Why? No matter how many setState() calls in how many components you do inside a React event handler, they will produce only a single re-render at the end of the event. This is crucial for good performance in large applications because if Child and Parent each call setState() when handling a click event, you don't want to re-render the Child twice.
An example of state being async. Note that alert shows value that was "closed over" when the button was clicked, not the current value in state.
To avoid needless re-renders, avoid calling setState when data hasn't changed (or use shouldComponentUpdate)
Hooks considered harmful - Covers how closures give functions state, how react-is determines if the dep array has changed, properly specifying dependency array.
Boilerplate for every data fetch call: 41mins here - Here's a pic of the spot Contrast with UseEffect to see how much simpler it is and how it unifies these concerns.
Do I need to specify functions as effect dependencies or not? Options, in order of preference:
Hoist functions that don’t need props or state outside of your component
If after that your effect still ends up using functions in the render scope (including function from props), wrap them into useCallback where they’re defined, and repeat the process. Why does it matter? Functions can “see” values from props and state — so they participate in the data flow.
With useState, you have to remember to use the function form to reliably set state based on current state. With useReducer, you can't mess this up. It's designed for updating values based upon previous state. Related post
Easy to test in isolation, since it's a pure function
Centralizes state writes, which is often easier to understand
Typically simplifies useEffect by radically simplifying the dependency array and nested logic as shown here
Easily handle related state (though admittedly you can also use objects with useState to compose data that changes together)
Can easily store entire state tree in localStorage
Can pass dispatch down to avoid passing many callback funcs on props
Can easily log all actions since they're centralized
Makes the component easier to digest by moving state management concerns to a separate file
Tip: Instead of useState, can do this: const [state, update] = useReducer((_, next) => next, initial);. Then can refactor to more robust logic easily. Otherwise, works like useState.
Think of useReducer as the “cheat mode” of Hooks. You can decouple the update logic from describing what happened. This helps remove unnecessary dependencies from effects and avoids re-running them more often than necessary.
Eliminate dependencies by using functional setState in useEffect, or when setting a state variable depends on the current value of another state variable, you might want to try replacing them both with useReducer. When you find yourself writing setSomething(something => ...), it’s a good time to consider using a reducer instead. A reducer lets you decouple expressing the “actions” that happened in your component from how the state updates in response to them.
You may omit dispatch, setState, and useRef container values from the deps because React guarantees them to be static. But it also doesn’t hurt to specify them.
useDeferredValue is like useTransition because it's also for low priority updates, but it's useful when the value is coming in "from above"/on props. (so, when your component doesn't have control over the setState call - perhaps a third party lib or package too). Note: You need to use memoization on the component to get any benefit. And the memoization value obviously shouldn't change too often or the benefit disappears.
useReducer tips: (below are from redux's style guide, but relevant)
useEffect is taught too much / too early. It's much more important to teach ways to avoid useEffect: derived state, single source of truth for state, react keys, callback refs, useSyncExternalStore even... know about them and you likely will not need useEffect.
WARNING: Almost none of the effects I see in app codebases are syncing with something outside of React. It's always: if prop change, update state, if state changes, dispatch another action, or if state changes, compute something and put in another state. All of these are bad.
useEffect is the reduce of hooks. You can do anything with it. You probably shouldn't be using it. There are better abstractions.
React Fundamentals
JSX
State
Forms
Functional components
Class Components
this
keywordTypechecking
Props
Keys
Hooks
useEffect
by radically simplifying the dependency array and nested logic as shown hereconst [state, update] = useReducer((_, next) => next, initial);
. Then can refactor to more robust logic easily. Otherwise, works like useState.startTransition
andpending
. The returnedstartTransition
says "This has a lower priority than other state updates". Typing into an input field should feel instant, so to support that, mark other state updates as low priority (viastartTransition
as needed. A filtering demo that shows that using useTransition for a filter term means the input stays responsive while the list update lags slightly behinduseReducer tips: (below are from redux's style guide, but relevant)
useEffect Best practices
reduce
of hooks. You can do anything with it. You probably shouldn't be using it. There are better abstractions.Component Composition
Error handling
Best practices
Quiz
The text was updated successfully, but these errors were encountered: