function ProductPage({ productId }) {
const [product, setProduct] = useState(null);
async function fetchProduct() {
const response = await fetch('http://myapi/product/' + productId); // Uses productId prop const json = await response.json();
setProduct(json);
}
useEffect(() => {
fetchProduct();
}, []); // 🔴 Invalid because `fetchProduct` uses `productId` // ...
}
function ProductPage({ productId }) {
const [product, setProduct] = useState(null);
useEffect(() => {
// By moving this function inside the effect, we can clearly see the values it uses. async function fetchProduct() { const response = await fetch('http://myapi/product/' + productId); const json = await response.json(); setProduct(json); }
fetchProduct();
}, [productId]); // ✅ Valid because our effect only uses productId // ...
}
function Example({ someProp }) {
useEffect(() => {
function doSomething() {
console.log(someProp); }
doSomething();
}, [someProp]); // ✅ OK (our effect only uses `someProp`)}
useEffect(() => {
function doSomething() {
console.log('hello');
}
doSomething();
}, []); // ✅ OK in this example because we don't use *any* values from component scope
function ProductPage({ productId }) {
// ✅ Wrap with useCallback to avoid change on every render const fetchProduct = useCallback(() => { // ... Does something with productId ... }, [productId]); // ✅ All useCallback dependencies are specified
return <ProductDetails fetchProduct={fetchProduct} />;
}
function ProductDetails({ fetchProduct }) {
useEffect(() => {
fetchProduct();
}, [fetchProduct]); // ✅ All useEffect dependencies are specified
// ...
}
useEffect(() => {
let ignore = false; async function fetchProduct() {
const response = await fetch('http://myapi/product/' + productId);
const json = await response.json();
if (!ignore) setProduct(json); }
fetchProduct();
return () => { ignore = true }; }, [productId]);
function Example({ someProp }) {
function doSomething() {
console.log(someProp); }
useEffect(() => {
doSomething();
}, []); // 🔴 This is not safe (it calls `doSomething` which uses `someProp`)}
Recommend
React Hooks FAQ From Classes to Hooks How can I measure a DOM node?
React Hooks FAQ From Classes to Hooks Is there something like forceUpdate?
React Hooks FAQ From Classes to Hooks How do I implement getDerivedStateFromProps?
React Hooks FAQ From Classes to Hooks Why am I seeing stale props or state inside my function?
React Hooks FAQ From Classes to Hooks How to get the previous props or state?
React Hooks FAQ From Classes to Hooks Should I use one or many state variables?
React Hooks FAQ From Classes to Hooks Is there something like instance variables?
React Hooks FAQ Adoption Strategy How to test components that use Hooks?
React Forms Controlled Input Null Value
React Forms Handling Multiple Inputs
React Forms The file input Tag
React Forms Controlled Components
Thinking in React Start With A Mock
React Typechecking With PropTypes Function Components
React Typechecking With PropTypes Default Prop Values
React Typechecking With PropTypes Requiring Single Child
React Typechecking With PropTypes PropTypes
React Typechecking With PropTypes
React Lifting State Up Lifting State Up
React Lifting State Up Writing Conversion Functions
React Lifting State Up Adding a Second Input
ReactDOM Reference createPortal()
ReactDOM Reference findDOMNode()
ReactDOM Reference unmountComponentAtNode()
React Release Channels Next Channel Using the Next Channel for Integration Testing
React Uncontrolled Components The file input Tag
React Uncontrolled Components Default Values
React CDN Links Why the crossorigin Attribute?
React Forwarding Refs Displaying a custom name in DevTools
React Forwarding Refs Forwarding refs in higher-order components
React Forwarding Refs Forwarding refs to DOM components
React Using the Effect Hook Tips for Using Effects Tip: Optimizing Performance by Skipping Effects
React Using the Effect Hook Tips for Using Effects Explanation: Why Effects Run on Each Update
React Using the Effect Hook Tips for Using Effects Tip: Use Multiple Effects to Separate Concerns
React Using the Effect Hook Recap
React Using the Effect Hook Effects with Cleanup Example Using Hooks
React Using the Effect Hook Effects with Cleanup Example Using Classes
React Using the Effect Hook Effects Without Cleanup Detailed Explanation
React Using the Effect Hook Effects Without Cleanup Example Using Hooks
React Using the Effect Hook Effects Without Cleanup Example Using Classes
React Hooks API Reference Additional Hooks useDebugValue Defer formatting debug values
React Hooks API Reference Additional Hooks useDebugValue
React Hooks API Reference Additional Hooks useImperativeHandle