Next.js: React Strict Mode causes re renders

Created on 30 Jan 2020  路  1Comment  路  Source: vercel/next.js

Bug report

Describe the bug

It appears that when reactStrictMode is set to true in next.config.js, components are rendered twice when using react hooks.

To Reproduce

pages/index.js

const Index = () => {
    const [count, setCount] = useState(0);
    console.log('render');

    return (
        <div>
            <p>You clicked {count} times</p>
            <button onClick={() => setCount(count + 1)}>Click me</button>
        </div>
    );
};

next.config.js

module.exports = {
    reactStrictMode: true
}

On load, you'll see it render twice. When you click, it'll render twice for each click.

Expected behavior

Should render once for each state change

System information

  • OS: macOS
  • Browser Chrome
  • Version of Next.js v9.2.1

Most helpful comment

This is how StrictMode is designed to work (the double renders are a React feature)! The docs for StrictMode explain the reasoning.

tl;dr:

Strict mode can鈥檛 automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following methods [...]

This behavior ensures your renders do not produce side-effects.

>All comments

This is how StrictMode is designed to work (the double renders are a React feature)! The docs for StrictMode explain the reasoning.

tl;dr:

Strict mode can鈥檛 automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following methods [...]

This behavior ensures your renders do not produce side-effects.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

formula349 picture formula349  路  3Comments

YarivGilad picture YarivGilad  路  3Comments

jesselee34 picture jesselee34  路  3Comments

timneutkens picture timneutkens  路  3Comments

renatorib picture renatorib  路  3Comments