DEV Community

Erik W.
Erik W.

Posted on

React: useEffect() Dependencies Questions?

I'm watching the video:
React JS Full Course for Beginners | Complete All-in-One Tutorial | 9 Hours

https://www.youtube.com/watch?v=RVFAyFWO4go

(Specifically, Chapter 19: Axios API Requests at 6:29:42)

I tried to get ahold of the video’s author, but no luck…

The example shown in the video uses the following code:

const EditPost = ({
  posts, handleEdit, editBody, setEditBody, editTitle, setEditTitle
}) => {
    const { id } = useParams();
    const post = posts.find(post => (post.id).toString() === id);

    useEffect(() => {
        if (post) {
            setEditTitle(post.title);
            setEditBody(post.body);
        }
    }, [post, setEditTitle, setEditBody])

    return (
        <div> nothing here yet... </div>
    )
}
Enter fullscreen mode Exit fullscreen mode

Why are post, setEditTitle, and setEditBody included in the useEffect() dependency array? What effect does including them have? Why is useEffect() needed at all here? Can't we just set the values directly?

Thanks, just please be sure to review CH 19 in this video, it will explain everything that I am asking about...

Top comments (1)

Collapse
 
christopher_lee_8d7e41fe1 profile image
Christopher Lee

Hi Nice to meet you