From 8bc4d932b20079c01045248fc02b2726ba73775c Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 24 Oct 2019 13:16:38 +0300 Subject: Correctly change state from useEffect without dependencies --- vnext/src/ui/Thread.js | 19 +++++++++------- vnext/src/ui/UserInfo.js | 56 ++++++++++++++++++++++++++---------------------- 2 files changed, 41 insertions(+), 34 deletions(-) (limited to 'vnext/src') diff --git a/vnext/src/ui/Thread.js b/vnext/src/ui/Thread.js index cbd40c4c..2e07cd0e 100644 --- a/vnext/src/ui/Thread.js +++ b/vnext/src/ui/Thread.js @@ -46,17 +46,20 @@ function Comment({ msg, draft, visitor, active, setActive, onStartEditing, postC }, []); useEffect(() => { isMounted = true; - if (author.uri) { - fetchUserUri(author.uri).then(response => { - if (isMounted) { - setAuthor(response.data); - } - }); - } + setAuthor(previous => { + if (previous.uri) { + fetchUserUri(previous.uri).then(response => { + if (isMounted) { + return response.data; + } + }); + } + return previous; + }); return () => { isMounted = false; }; - }, [author.uri]); + }, []); return (