Browse Source

[Beta] Reusing Logic with Custom Hooks (#4958)

main
dan 2 years ago
committed by GitHub
parent
commit
59c68a7baf
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      beta/src/pages/learn/removing-effect-dependencies.md
  2. 1407
      beta/src/pages/learn/reusing-logic-with-custom-hooks.md
  3. 4
      beta/src/sidebarLearn.json

2
beta/src/pages/learn/removing-effect-dependencies.md

@ -547,7 +547,7 @@ function ShippingForm({ country }) {
Now the first Effect only re-runs if the `country` changes, while the second Effect re-runs when the `city` changes. You've separated them by purpose: two different things are synchronized by two separate Effects. Two separate Effects have two separate dependency lists, so they will no longer trigger each other unintentionally.
The final code is longer than the original, but splitting these Effects is still correct. [Each Effect should represent an independent synchronization process.](/learn/lifecycle-of-reactive-effects#each-effect-represents-a-separate-synchronization-process) In this example, deleting one Effect doesn't break the other Effect's logic. This is a good indication that they *synchronize different things,* and it made sense to split them up.
The final code is longer than the original, but splitting these Effects is still correct. [Each Effect should represent an independent synchronization process.](/learn/lifecycle-of-reactive-effects#each-effect-represents-a-separate-synchronization-process) In this example, deleting one Effect doesn't break the other Effect's logic. This is a good indication that they *synchronize different things,* and it made sense to split them up. If the duplication feels concerning, you can further improve this code by [extracting repetitive logic into a custom Hook.](/learn/reusing-logic-with-custom-hooks#when-to-use-custom-hooks)
### Are you reading some state to calculate the next state? {/*are-you-reading-some-state-to-calculate-the-next-state*/}

1407
beta/src/pages/learn/reusing-logic-with-custom-hooks.md

File diff suppressed because it is too large

4
beta/src/sidebarLearn.json

@ -182,6 +182,10 @@
{
"title": "Removing Effect Dependencies",
"path": "/learn/removing-effect-dependencies"
},
{
"title": "Reusing Logic with Custom Hooks",
"path": "/learn/reusing-logic-with-custom-hooks"
}
]
}

Loading…
Cancel
Save