-
Notifications
You must be signed in to change notification settings - Fork 151
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
fix: memoized otp input and added example for using onOTPFilled #2497
base: master
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: 960c115 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ PR title follows Conventional Commits specification. |
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 960c115:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tewarig what is this PR? were there some perf issues?
@@ -0,0 +1,5 @@ | |||
--- | |||
'@razorpay/blade': major |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
major :o
it was related to this |
@@ -419,7 +419,7 @@ const _OTPInput: React.ForwardRefRenderFunction<HTMLInputElement[], OTPInputProp | |||
); | |||
}; | |||
|
|||
const OTPInput = React.forwardRef<HTMLInputElement[], OTPInputProps>(_OTPInput); | |||
const OTPInput = memo(React.forwardRef<HTMLInputElement[], OTPInputProps>(_OTPInput)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the cause of re-renders though? Shouldn't it not re-render independent of whether we have memo or not?
Description
This PR fixes the issue with the OTPInput component where the onOTPFilled callback was causing infinite re-renders.
https://stackblitz.com/edit/react-hsrtdxhg?file=App.js
To avoid re-renders, we can wrap OTPInput in React.memo. I have implemented this. Additionally, the function passed to onOTPFilled should be wrapped in useCallback. I have added an example for this in the documentation.
Changes
Additional Information
Component Checklist