From a652d427ebd966251d77e53a4c8994f0542ca7b5 Mon Sep 17 00:00:00 2001 From: Kier Borromeo Date: Sat, 4 Apr 2020 04:54:30 +0800 Subject: [PATCH] Add deps to useEffect --- react.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/react.md b/react.md index 3aae19340..2aa7db645 100644 --- a/react.md +++ b/react.md @@ -365,7 +365,7 @@ function Example() { useEffect(() => { // Update the document title using the browser API document.title = `You clicked ${count} times`; - }); + }, [count]); return (
@@ -379,7 +379,7 @@ function Example() { ``` {: data-line="6,7,8,9,10"} -If you’re familiar with React class lifecycle methods, you can think of `useEffect` Hook as `componentDidMount`, `componentDidUpdate`, and `componentWillUnmount` combined. +If you’re familiar with React class lifecycle methods, you can think of `useEffect` Hook as `componentDidMount`, `componentDidUpdate`, and `componentWillUnmount` combined. By default, React runs the effects after every render — including the first render. @@ -401,7 +401,7 @@ function FriendStatus(props) { return () => { ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange); }; - }); + }, [props.friend.id]); if (isOnline === null) { return 'Loading...';