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

[Feature Request]: Add array of updated deps indices to useEffect hooks arg #194

Closed
myckhel opened this issue May 26, 2021 · 1 comment
Closed

Comments

@myckhel
Copy link

myckhel commented May 26, 2021

I would like to request adding the indices of updated dependencies of useEffect, useLayoutEffect as arg of the callback.

For example:

if dep1 and dep3 changes, then indices of the dependencies that triggers the update should be passed to the callback as argument.

useEffect((updatedIndices) => {

  console.log(updatedIndices); // Array [1,3]

}, [dep1, dep2, dep3])

I think this feature will be useful for most of us all.

This is just one example use case

Allow component modify prop internally

Good

const InternalState = ({value, checked}) => {
  const [_checked, setChecked] = useState(checked)
  const [_value, setValue] = useState(value)
  
  // better
  useEffect((indices) => {
    if(indices === 1){
      setValue(value + ' appends');
     }
    
     if(indices === 2){
      setChecked(checked + ' appends');
     }
  }, [value, checked])

}

Can improve

  useEffect(() => {
      setValue(value + ' appends');
  }, [value])
  
  // can improve
  useEffect(() => {
      setChecked(checked + ' appends')
  }, [checked])

Bad

  useEffect(() => {
    // possible unnecessary state update if value not changed
    setValue(value + ' appends');

    // possible unnecessary state update if checked not changed
    setChecked(checked + ' appends');
  }, [value, checked])

My actual use case would be too complicated to show here.

@gaearon
Copy link
Member

gaearon commented Aug 24, 2021

Hi, thanks for your suggestion. RFCs should be submitted as pull requests, not issues. I will close this issue but feel free to resubmit in the PR format.

@gaearon gaearon closed this as completed Aug 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants