Skip to content

[Compiler Bug]: environment.validateNoSetStateInRender doesn't throw an error for wrong case #36279

@dimaMachina

Description

@dimaMachina

What kind of issue is this?

  • React Compiler core (the JS output is incorrect, or your app works incorrectly after optimization)
  • babel-plugin-react-compiler (build issue installing or using the Babel plugin)
  • eslint-plugin-react-hooks (build issue installing or using the eslint plugin)
  • react-compiler-healthcheck (build issue installing or using the healthcheck script)

Link to repro

https://playground.react.dev/#N4Igzg9grgTgxgUxALhASwLYAcIwC4AEwBUYCAyngIZ4IEC+BAZjBBgQOQwJVx4cBuADoA7UUygi+aCCIIAxNABsEAFW4IAFAEoioggTiywhYrzxoAbggAiEOADkqGOowC8JMnbhQXIvADCsrQAHng6wnKGxoQA2ljclgCC0tbeTi4ANARkeAAKiSkWafYZCAC6BB6kFNS0muZWtqXOCNqR+gRoTAQNqc2OrQQAhG4eCQjJ-emtusCdBrkFk0VNMy59xQNl7Z30op3ceLByADwAJlYEAPQAfKL7IiCZIEYiTGgA5igg81FCIAQIksaFYIj8eAByD0YhAlioSjQ5xoCAcEHICDwlBRAEkRAAlIHnBAwKEEPAwKAIPYPZ7gAAWEAA7njaDARAiwCgmJyEPQgA

Repro steps

Replace if (activeDocName !== prevActiveDocName) { with if (true) { and you will receive

Found 1 error:

Error: Cannot call setState during render

Calling setState during render may trigger an infinite loop.
* To reset state when other state/props change, store the previous value in state and update conditionally: https://react.dev/reference/react/useState#storing-information-from-previous-renders
* To derive data from other state/props, compute the derived data during render without using state.

   6 |
   7 |   if (true) {
>  8 |     setPrevActiveDocName(activeDocName);
     |     ^^^^^^^^^^^^^^^^^^^^ Found setState() in render
   9 |   }
  10 |
  11 |   return <div />

I expected to receive the same error when using the condition if (activeDocName !== prevActiveDocName) {.

cc @sleitor tagging you if it something that you want to fix based on your previous amazing contributions 🙏

How often does this bug happen?

Every time

What version of React are you using?

latest

What version of React Compiler are you using?

latest

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: UnconfirmedA potential issue that we haven't yet confirmed as a bugType: Bug

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions