Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Rule: "react/no-unused-state": TypeError: Cannot read properties of undefined (reading 'references') #3568

Closed
2 tasks done
yurigenin opened this issue May 1, 2023 · 1 comment
Assignees
Labels

Comments

@yurigenin
Copy link

Is there an existing issue for this?

  • I have searched the existing issues and my issue is unique
  • My issue appears in the command-line and not only in the text editor

Description Overview

When the prev state parameter (second parameter) of getDerivedStateFromProps is destructured in-place, the rule react/no-unused-state crashes

static getDerivedStateFromProps = ({value, disableAnimation}: ToggleProps, {isControlled, isOn}: ToggleState) => {

here, isControlled and isOn are destructured state properties.

TypeError: Cannot read properties of undefined (reading 'references')
Occurred while linting /Users/ygenin/src/pillar/src/datagrid/implementation/filtering/filterEditors/DateFilterEditor.tsx:57
Rule: "react/no-unused-state"
    at PropertyDefinition, ClassProperty (/Users/ygenin/src/pillar/node_modules/eslint-plugin-react/lib/rules/no-unused-state.js:388:34)

Expected Behavior

The eslint rule should handle the code without crashing.

The issue seems to be that the stateArg.name on the line below is undefined whereas it is expected to be 'state' or some other name if destructuring is not used.

const argVar = scope.variables.find((x) => x.name === stateArg.name);

eslint-plugin-react version

7.32.2

eslint version

8.2.0

node version

18.12.1

@yurigenin yurigenin added the bug label May 1, 2023
@ljharb ljharb self-assigned this May 1, 2023
@ljharb
Copy link
Member

ljharb commented May 1, 2023

Although lifecycles should always be a method and never a class field, it shouldn't crash regardless, so I'll have a fix pushed up shortly.

ljharb added a commit to ljharb/eslint-plugin-react that referenced this issue May 1, 2023
@ljharb ljharb closed this as completed in f2d8d26 May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants