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.

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.

A girl receiving new message

Sign to our Newsletter

We're committed to your privacy. Curiosum uses the information you provide to us to contact you about our relevant content. You may unsubscribe from these communications at any time. For more information, check out our privacy policy.

Szymon Soppa Web Developer
Szymon Soppa Curiosum Founder & CEO

Read more
on #curiosum blog

Phoenix LiveView Tutorial: Adding Phoenix PubSub and Pow Authentication to Messenger
  • Elixir

Phoenix LiveView Tutorial: Adding Phoenix PubSub and Pow Authentication to Messenger

We've already bootstrapped our Phoenix LiveView-based Messenger app's database structure and a first LiveView page.

This time, we're going to improve real-time communication between the app's users using Phoenix PubSub, and use the Pow library to add secure user authentication.

As of November 2020, the latest Phoenix LiveView version is 0.14.8 - and the series has been updated to match it!

5 top-tier companies that use Elixir
  • Elixir

5 top-tier companies that use Elixir

Elixir is a pretty capable language - and it consistently ranks near the top of most loved and wanted languages rankings. It has a large following and some very persuasive preachers as well. But that would not be enough to make me like it – what I need as real proof of its strengths is real businesses that strive with Elixir.

That’s what this list is all about – a bunch of stories from top companies that chose Elixir and never looked back. Let us show you how its power and versatility shows in practice.