How to call useEffect React Hook on a component mount and unmount

With pure function React Components you're not allowed to use lifecycle methods like componentDidMount or componentWillUnmount.

Table of contents

    These can be replaced with proper use of useEffect hook introduced in React version 16.8.

    Here is the code that will run exactly once when a component is mounted and exactly once when it's supposed to be unmounted:

    import { useEffect } from "React";
    const ExampleComponent = () => {
      useEffect(() => {
        // Here goes the code you wish to run on mount
        return () => {
          // Here goes the code you wish to run on unmount
      }, []);

    It's crucial to pass an empty array as the second argument of useEffect function call. If you omit it or pass dependencies inside of array it won't work as intended.

    Download our ebook
    Szymon Soppa Web Developer
    Szymon Soppa Curiosum Founder & CEO

    Read more
    on #curiosum blog

    7 Reasons Why You Might Need a Custom Marketing Tool

    Managing a marketing agency or introducing a company to digital transformation in the area of marketing, requires an extraordinary focus on the tools that you should use. Usually, they facilitate the work of marketers and allow you to achieve the expected results of each project and advertising campaign you are working on.

    How to upload a file in Elixir with Waffle

    The ability to upload files is a key requirement for many todays web and mobile applications. In this tutorial, we will look at how we can accomplish file uploads to local storage and S3 server in Phoenix with the help of Waffle library.