Skip to content

fix(eslint-plugin-react-hooks): Support nullish coalescing and optional chaining of dependencies#19008

Merged
gaearon merged 1 commit into
react:masterfrom
yanneves:fix/hooks-nullish-coalescing
May 27, 2020
Merged

fix(eslint-plugin-react-hooks): Support nullish coalescing and optional chaining of dependencies#19008
gaearon merged 1 commit into
react:masterfrom
yanneves:fix/hooks-nullish-coalescing

Conversation

@yanneves

@yanneves yanneves commented May 26, 2020

Copy link
Copy Markdown
Contributor

Relates to #18985

Summary

In conditions where optional chaining is used inside the useEffect hook, the react-hooks/exhaustive-deps rule falsely flags missing dependencies. This change adds some logic to the linting rules to also match dependencies with optional chaining.

Test Plan

Have added tests to cover the following valid scenarios that were previously failing:

// Nullish coalescing
function MyComponent(props) {
  useEffect(() => {
    console.log(props.foo?.bar?.baz ?? null);
  }, [props.foo]);
}
// Optional chaining
function MyComponent(props) {
  useEffect(() => {
    console.log(props.foo?.bar);
  }, [props.foo?.bar]);
}
// Normal and optional chaining
function MyComponent(props) {
  useEffect(() => {
    console.log(props.foo);
    console.log(props.foo?.bar);
  }, [props.foo]);
}
// Optional chaining with prototype method
function MyComponent(props) {
  useEffect(() => {
    console.log(props.foo?.toString());
  }, [props.foo]);
}

@codesandbox-ci

codesandbox-ci Bot commented May 26, 2020

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 6faded2:

Sandbox Source
vigilant-mcclintock-rmoly Configuration

@sizebot

sizebot commented May 26, 2020

Copy link
Copy Markdown

No significant bundle size changes to report.

Size changes (experimental)

Generated by 🚫 dangerJS against 6faded2

@sizebot

sizebot commented May 26, 2020

Copy link
Copy Markdown

No significant bundle size changes to report.

Size changes (stable)

Generated by 🚫 dangerJS against 6faded2

… and optional chaining of dependencies, relates to react#18985
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants