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

[RFC] Stabilize the useInput return properties #8780

Closed
wants to merge 1 commit into from

Conversation

Nickman87
Copy link

I was writing some more complex custom components inside of react-admin that use useInput to update fields.
Somewhere in my code I use an effect, based on the results of this hook, to trigger updates like so:

const [someState] = useState("someValue");
const testField = useInput({source: "test"});

useEffect(() => {
  if (testField.field.value !== someState) {
    testField.field.onChange(someState);
  }
}, [someState, testField.field.value, testField.field.onChange]);

(I know this might look like stupid code, but this is a very trimmed down example of what I want to achieve in my component:) )

This resulted in an infinite loop because the onChange that is returned from useInput is not stable, it is always a new function, even though nothing significant changes.

I made a quick fix for this in this PR, will add tests or more fixes if this is deemed to be a good fix or not. Not sure if there are further implications or reasonings why this is done the way it is now, so looking for feedback.

@slax57
Copy link
Contributor

slax57 commented Mar 31, 2023

I believe that's because we never had a use-case where this would be a problem, but currently I see no good reason not to memoize the onChange and onBlur callbacks.
I'm just not a huge fan of the name stableOnChange. You should just call them onChange and onBlur IMO.
But that's definitely a PR I would approve if it doesn't break anything!
Thanks for submitting it!

fzaninotto added a commit that referenced this pull request Apr 11, 2023
@fzaninotto
Copy link
Member

fzaninotto commented Apr 11, 2023

I made another PR that should solve the problem without adding new props. Thanks for bringing attention to this problem!

@fzaninotto fzaninotto closed this Apr 11, 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.

3 participants