question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unnecessary re-renderings

See original GitHub issue

Please examine this code snippet.

The content of the snippet is simple and looks like this:

import React from "react";

import { atom, useRecoilValue } from "recoil";

const exampleState = atom({
  key: "exampleState",
  default: "some value"
});

export default function App() {
  const value = useRecoilValue(exampleState);

  console.log("check render");  // it logs 4 times

  return <div>{value}</div>;
}

There is nothing special (I guess), one atom and one useRecoilValue inside the only component. The problem is that the console.log that we have in the component logs 4 times

useRecoilValue (without updating the atom) re-renders the component (which uses it) 4 times in the beginning. If we change our code a little bit to have further updates of the atom we can see 2 more logs after each update 😕

import React, { useEffect } from "react";

import { atom, useRecoilState } from "recoil";

const exampleState = atom({
  key: "exampleState",
  default: "some value"
});

export default function App() {
  const [value, setValue] = useRecoilState(exampleState);

  useEffect(() => {
    const timerId = setInterval(() => {
      setValue("other value");
    }, 2000);

    return () => clearInterval(timerId);
  }, [setValue]);

  console.log("check render"); // it logs 4 times at the beginig
  // plus 2 more logs after atom's every update

  return <div>{value}</div>;
}

Here is the snippet for this one.

In the ideal (normal) case I suspect to see one log (with ‘some value’) and after about 2 seconds one more log (with ‘other value’) and that’s all because further updates don’t change the value.

I’ve found this issue and some others related to it, but they are about selector optimization. Here we have not any selector, there is just one atom and its usage in the component (via useRecoilValue/useRecoilState)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
davidmccabecommented, Sep 10, 2020

Ah, an extra render on mount is expected when using React 16. It is eliminated when using new experimental React APIs.

1reaction
suren-atoyancommented, Sep 10, 2020

But there is a huge improvement regarding further updates

  1. we get just one update after each setValue
  2. we don’t get any update if the new value is equivalent to the previous one

So, that’s really great 🎉 only remains the initial render issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimizing React performance by preventing unnecessary re ...
Re -rendering React components unnecessarily can slow down your app and make the UI feel unresponsive. This article explains how to update ...
Read more >
5 Ways to Avoid React Component Re-Renderings
In this article, I have discussed 5 different methods to prevent unnecessary re-rendering in React components. Most of these solutions capitalize caching, and ......
Read more >
React re-renders guide: everything, all at once - Developer way
Necessary re-render - re-render of a component that is the source of the changes, or a component that directly uses the new information....
Read more >
How I eliminate ALL unnecessary Rerenders in React - Medium
The two most common causes of unnecessary unmounting are: Not using event. preventDefault()
Read more >
Just Say No to Excessive Re-Rendering in React - GrapeCity
Unnecessary renders occur when child components go through the render phase but not the commit phase. One way to fix this, as shown...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found