Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions beta/src/content/learn/you-might-not-need-an-effect.md
Original file line number Diff line number Diff line change
Expand Up @@ -597,13 +597,21 @@ function Parent() {
}

function Child({ onFetched }) {
const data = useSomeAPI();

// 🔴 Avoid: Passing data to the parent in an Effect
useEffect(() => {
if (data) {
onFetched(data);
}
}, [onFetched, data]);
let canRun = true;

useSomeAPI().then(data => {
if (data && canRun) {
onFetched(data);
}
});

// clean up
return () => (canRun = false);
}, [onFetched]);

// ...
}
```
Expand All @@ -612,7 +620,21 @@ In React, data flows from the parent components to their children. When you see

```js {4-5}
function Parent() {
const data = useSomeAPI();
const [data, setData] = useState(null);

useEffect(() => {
let canRun = true;

useSomeAPI().then(fetchedData => {
if (fetchedData && canRun) {
setData(fetchedData);
}
});

// clean up
return () => (canRun = false);
}, []);

// ...
// ✅ Good: Passing data down to the child
return <Child data={data} />;
Expand Down