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

Remove unnecessary useEffect hook #750

Closed
wants to merge 1 commit into from
Closed

Conversation

aashu16
Copy link
Collaborator

@aashu16 aashu16 commented Apr 12, 2023

Describe the issue/change

Part of the logic in the NumberFormatBase component that is responsible for handling prop changes does not need to be wrapped in a useEffect hook. Moving this logic outside the hook also ensures that it runs before the component is rendered, preventing unnecessary re-renders.

Describe the changes proposed/implemented in this PR

This PR does the following:

  • Move the logic outside the useEffect hook.
  • Move the affected code so that it follows the declaration of any values/functions that it uses/calls.

The affected logic does not need to be wrapped in a useEffect hook for
it to be executed on prop change. Moving this logic outside the hook
also ensures that it runs before the component is rendered, preventing
unnecessary re-renders.

This commit also moves the affected chunk of code so that it follows the
declaration of any values/functions that it uses/calls.
@s-yadav
Copy link
Owner

s-yadav commented May 1, 2023

useEffect here is intentional, as you can see on failing test.
There are two reasons,

  1. As this takes care of informing the parent about formatted value change on prop change. Changing parent state while rendering children is invalid and can cause unexpected bugs.

  2. Also, the updateValue depends on the internal state and the internal state change is only reflected after the react bails out and try to render again, but the logic inside component execution doesn't stop, so it can call onValueChange callback twice. Using useEffect here make sure, it happens after the state is stable and applied to dom.

Take this example.

  • props changed due to change in parent.
  • number format tries to update internal state based on prop.
  • updateValue gets called with previous state and new props
  • internal state updates
  • updateValue gets called with new state and new props

@s-yadav
Copy link
Owner

s-yadav commented May 1, 2023

However this can be optimized with some refactor. to avoid 2 renders within react number format component. But we still have to inform the parent with onValueChange on useEffect.

@aashu16
Copy link
Collaborator Author

aashu16 commented May 8, 2023

Been thinking about the best way to tackle this. I will try and push an update before the end of the week.

@s-yadav
Copy link
Owner

s-yadav commented May 9, 2023

@aashu16 I have fixed this on ongoing fixes. yet to merge and release.

@aashu16
Copy link
Collaborator Author

aashu16 commented May 9, 2023

Oh cool. I already started working on a fix yesterday, but no worries!

Have you pushed those changes to a remote branch yet? Just wondering if I could take a look at the approach you took.

@aashu16 aashu16 closed this May 25, 2023
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

Successfully merging this pull request may close these issues.

2 participants