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
Copy file name to clipboardExpand all lines: beta/src/pages/learn/you-might-not-need-an-effect.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,7 +93,7 @@ function TodoList({ todos, filter }) {
93
93
94
94
In many cases, this code is fine! But maybe `getFilteredTodos()` is slow or you have a lot of `todos`. In that case you don't want to recalculate `getFilteredTodos()` if some unrelated state variable like `newTodo` has changed.
95
95
96
-
You can cache (or ["memoize"](https://en.wikipedia.org/wiki/Memoization)) an expensive calculation by wrapping it [`useMemo`](/apis/usememo) Hook:
96
+
You can cache (or ["memoize"](https://en.wikipedia.org/wiki/Memoization)) an expensive calculation by wrapping it in a [`useMemo`](/apis/usememo) Hook:
97
97
98
98
```js {5-8}
99
99
import { useMemo, useState } from'react';
@@ -250,7 +250,7 @@ Now there is no need to "adjust" the state at all. If the item with the selected
250
250
251
251
### Sharing logic between event handlers {/*sharing-logic-between-event-handlers*/}
252
252
253
-
Let's say you have a product page with two buttons (Buy and Checkout) that both let you buy that product. You want to show a notification [toast](https://uxdesign.cc/toasts-or-snack-bars-design-organic-system-notifications-1236f2883023) whenever the user puts the product in the cart. Adding the `showToast()` call to both button's click handlers feels repetitive so you might be tempted to place this logic in an Effect:
253
+
Let's say you have a product page with two buttons (Buy and Checkout) that both let you buy that product. You want to show a notification [toast](https://uxdesign.cc/toasts-or-snack-bars-design-organic-system-notifications-1236f2883023) whenever the user puts the product in the cart. Adding the `showToast()` call to both buttons' click handlers feels repetitive so you might be tempted to place this logic in an Effect:
254
254
255
255
```js {2-7}
256
256
functionProductPage({ product, addToCart }) {
@@ -358,7 +358,7 @@ When you choose whether to put some logic into an event handler or an Effect, th
358
358
359
359
### Initializing the application {/*initializing-the-application*/}
360
360
361
-
Some logic should only runs once when the app loads. You might place it in an Effect in the top-level component:
361
+
Some logic should only run once when the app loads. You might place it in an Effect in the top-level component:
362
362
363
363
```js {2-6}
364
364
functionApp() {
@@ -645,7 +645,7 @@ function SearchResults({ query }) {
645
645
}
646
646
```
647
647
648
-
This ensures that when you Effect fetches data, all responses except the last requested one will be ignored.
648
+
This ensures that when your Effect fetches data, all responses except the last requested one will be ignored.
649
649
650
650
Handling race conditions is not the only difficulty with implementing data fetching. You might also want to think about how to cache the responses (so that the user can click Back and see the previous screen instantly instead of a spinner), how to fetch them on the server (so that the initial server-rendered HTML contains the fetched content instead of a spinner), and how to avoid network waterfalls (so that a child component that needs to fetch data doesn't have to wait for every parent above it to finish fetching their data before it can start). **These issues apply to any UI library, not just React. Solving them is not trivial, which is why modern [frameworks](/learn/start-a-new-react-project#building-with-a-full-featured-framework) provide more efficient built-in data fetching mechanisms than writing Effects directly in your components.**
0 commit comments