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: eslint-plugin-react-hooks shows error for props functions #19829

Closed
syabro opened this issue Sep 14, 2020 · 2 comments
Closed

Bug: eslint-plugin-react-hooks shows error for props functions #19829

syabro opened this issue Sep 14, 2020 · 2 comments

Comments

@syabro
Copy link

syabro commented Sep 14, 2020

"eslint-plugin-react-hooks": "^4.1.2",

Steps To Reproduce

import React, { useCallback } from 'react'

export function CheckboxField(props) {
  const onChange = useCallback((e) => props.onChange(e.target.checked), [props.onChange])
  return <input type="checkbox" onChange={onChange} />
}

The current behavior

4:73 error React Hook useCallback has a missing dependency: 'props'.
Either include it or remove the dependency array. However, 'props' will change when any prop changes, so the preferred fix is to destructure the 'props' object outside of the useCallback call and refer to those specific props inside useCallback react-hooks/exhaustive-deps

The expected behavior

Error shouldn't be triggered since this code has a correct dependency of props.onChange

@syabro syabro added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Sep 14, 2020
@eps1lon
Copy link
Collaborator

eps1lon commented Sep 14, 2020

This is expected behavior since props.onChange() has this inside onChange pointing to props. Which means that the effect should close over up-to-date values of props.

The warning includes the current recommendation: destructure onChange so that it is statically analyzable that you have a stable this.

I understand that it is very uncommon to use this as props but if you're not actually doing it you might as well destructure.

The change was introduced in #14993. Previous discussion: #18533

@eps1lon eps1lon closed this as completed Sep 14, 2020
@eps1lon eps1lon added Resolution: Duplicate and removed Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug labels Sep 14, 2020
@syabro
Copy link
Author

syabro commented Sep 14, 2020

@eps1lon thanks will check original thread

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants