` to provide it from a parent.
304 | - Context passes through any components in the middle.
305 | - Context lets you write components that “adapt to their surroundings”.
306 | - Before you use context, try passing props or passing JSX as `children`.
307 |
308 | *link: https://react.dev/learn/passing-data-deeply-with-context*
309 |
310 |
311 |
312 |
313 | ### Scaling Up with Reducer and Context
314 |
315 | - You can combine reducer with context to let any component read and update state above it.
316 | - To provide state and the dispatch function to components below:
317 | 1. Create two contexts (for state and for dispatch functions).
318 | 2. Provide both contexts from the component that uses the reducer.
319 | 3. Use either context from components that need to read them.
320 | - You can further declutter the components by moving all wiring into one file.
321 | - You can export a component like `TasksProvider` that provides context.
322 | - You can also export custom Hooks like `useTasks` and `useTasksDispatch` to read it.
323 | - You can have many context-reducer pairs like this in your app.
324 |
325 | *link: https://react.dev/learn/scaling-up-with-reducer-and-context*
326 |
327 |
328 |
329 |
330 |
331 | ## Escape Hatches
332 |
333 | ### Referencing Values with Refs
334 |
335 | - Refs are an escape hatch to hold onto values that aren’t used for rendering. You won’t need them often.
336 | - A ref is a plain JavaScript object with a single property called `current`, which you can read or set.
337 | - You can ask React to give you a ref by calling the `useRef` Hook.
338 | - Like state, refs let you retain information between re-renders of a component.
339 | - Unlike state, setting the ref’s `current` value does not trigger a re-render.
340 | - Don’t read or write `ref.current` during rendering. This makes your component hard to predict.
341 |
342 | *link: https://react.dev/learn/referencing-values-with-refs*
343 |
344 |
345 |
346 |
347 | ### Manipulating the DOM with Refs
348 |
349 | - Refs are a generic concept, but most often you’ll use them to hold DOM elements.
350 | - You instruct React to put a DOM node into `myRef.current` by passing ``.
351 | - Usually, you will use refs for non-destructive actions like focusing, scrolling, or measuring DOM elements.
352 | - A component doesn’t expose its DOM nodes by default. You can opt into exposing a DOM node by using `forwardRef` and passing the second `ref` argument down to a specific node.
353 | - Avoid changing DOM nodes managed by React.
354 | - If you do modify DOM nodes managed by React, modify parts that React has no reason to update.
355 |
356 | *link: https://react.dev/learn/manipulating-the-dom-with-refs*
357 |
358 |
359 |
360 |
361 | ### Synchronizing with Effects
362 |
363 | - Unlike events, Effects are caused by rendering itself rather than a particular interaction.
364 | - Effects let you synchronize a component with some external system (third-party API, network, etc).
365 | - By default, Effects run after every render (including the initial one).
366 | - React will skip the Effect if all of its dependencies have the same values as during the last render.
367 | - You can’t “choose” your dependencies. They are determined by the code inside the Effect.
368 | - Empty dependency array (`[]`) corresponds to the component “mounting”, i.e. being added to the screen.
369 | - In Strict Mode, React mounts components twice (in development only!) to stress-test your Effects.
370 | - If your Effect breaks because of remounting, you need to implement a cleanup function.
371 | - React will call your cleanup function before the Effect runs next time, and during the unmount.
372 |
373 | *link: https://react.dev/learn/synchronizing-with-effects*
374 |
375 |
376 |
377 |
378 | ### You Might Not Need an Effect
379 |
380 | - If you can calculate something during render, you don’t need an Effect.
381 | - To cache expensive calculations, add `useMemo` instead of `useEffect`.
382 | - To reset the state of an entire component tree, pass a different `key` to it.
383 | - To reset a particular bit of state in response to a prop change, set it during rendering.
384 | - Code that runs because a component was _displayed_ should be in Effects, the rest should be in events.
385 | - If you need to update the state of several components, it’s better to do it during a single event.
386 | - Whenever you try to synchronize state variables in different components, consider lifting state up.
387 | - You can fetch data with Effects, but you need to implement cleanup to avoid race conditions.
388 |
389 | *link: https://react.dev/learn/you-might-not-need-an-effect*
390 |
391 |
392 |
393 |
394 | ### Lifecycle of Reactive Effects
395 |
396 | - Components can mount, update, and unmount.
397 | - Each Effect has a separate lifecycle from the surrounding component.
398 | - Each Effect describes a separate synchronization process that can _start_ and _stop_.
399 | - When you write and read Effects, think from each individual Effect’s perspective (how to start and stop synchronization) rather than from the component’s perspective (how it mounts, updates, or unmounts).
400 | - Values declared inside the component body are “reactive”.
401 | - Reactive values should re-synchronize the Effect because they can change over time.
402 | - The linter verifies that all reactive values used inside the Effect are specified as dependencies.
403 | - All errors flagged by the linter are legitimate. There’s always a way to fix the code to not break the rules.
404 |
405 | *link: https://react.dev/learn/lifecycle-of-reactive-effects*
406 |
407 |
408 |
409 |
410 | ### Separating Events from Effects
411 |
412 | - Event handlers run in response to specific interactions.
413 | - Effects run whenever synchronization is needed.
414 | - Logic inside event handlers is not reactive.
415 | - Logic inside Effects is reactive.
416 | - You can move non-reactive logic from Effects into Effect Events.
417 | - Only call Effect Events from inside Effects.
418 | - Don’t pass Effect Events to other components or Hooks.
419 |
420 | *link: https://react.dev/learn/separating-events-from-effects*
421 |
422 |
423 |
424 |
425 | ### Removing Effect Dependencies
426 |
427 | - Dependencies should always match the code.
428 | - When you’re not happy with your dependencies, what you need to edit is the code.
429 | - Suppressing the linter leads to very confusing bugs, and you should always avoid it.
430 | - To remove a dependency, you need to “prove” to the linter that it’s not necessary.
431 | - If some code should run in response to a specific interaction, move that code to an event handler.
432 | - If different parts of your Effect should re-run for different reasons, split it into several Effects.
433 | - If you want to update some state based on the previous state, pass an updater function.
434 | - If you want to read the latest value without “reacting” it, extract an Effect Event from your Effect.
435 | - In JavaScript, objects and functions are considered different if they were created at different times.
436 | - Try to avoid object and function dependencies. Move them outside the component or inside the Effect.
437 |
438 | *link: https://react.dev/learn/removing-effect-dependencies*
439 |
440 |
441 |
442 |
443 | ### Reusing Logic with Custom Hooks
444 |
445 | - Custom Hooks let you share logic between components.
446 | - Custom Hooks must be named starting with `use` followed by a capital letter.
447 | - Custom Hooks only share stateful logic, not state itself.
448 | - You can pass reactive values from one Hook to another, and they stay up-to-date.
449 | - All Hooks re-run every time your component re-renders.
450 | - The code of your custom Hooks should be pure, like your component’s code.
451 | - Wrap event handlers received by custom Hooks into Effect Events.
452 | - Don’t create custom Hooks like `useMount`. Keep their purpose specific.
453 | - It’s up to you how and where to choose the boundaries of your code.
454 |
455 | *link: https://react.dev/learn/reusing-logic-with-custom-hooks*
456 |
457 |
458 |
459 |
460 |
461 |
--------------------------------------------------------------------------------