Use a better example for Effect hook
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 11.8k
- Forks
- 7.9k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 11
Description
The following example is used in:
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`;
});
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
This example creates confusion because the example doesn't actually need useEffect to operate correctly.
If you set document.title outside of the useEffect, the app behaves exactly the same.
Suggested changes
-
Use an example which would not work correctly if it wasn't placed in a
useEffect. It should be something that depends on the DOM having been updated. Maybe setting thedocument.titleto adocument.getElementByIdor something like that. This would also be a contrived/useless example. Something practical would be better. -
Show the lifecycle of useEffect early. I was most interested in the lifecycle of when this method is called. In Dan's recent blog post on effects, this is the first thing that is covered, and it should be the same for these docs.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Effect Hook examples in hooks-overview.html#effect-hook and hooks-effect.html, then compare the current example with the lifecycle discussion in the referenced documentation. Replace the confusing example with one that demonstrates why useEffect is needed and introduce its lifecycle earlier; done means both pages consistently explain the behavior and show the improved example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100