-
Notifications
You must be signed in to change notification settings - Fork 8
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
Add onUpdate
callback
#9
base: main
Are you sure you want to change the base?
Conversation
Thank you! Just one question: Is there a risk that adding |
Good catch, we should probably wrap this callback in a ref, so we always get the latest value without impacting |
@ViktorQvarfordt done! |
src/auto-text-size-react.tsx
Outdated
|
||
useEffect(() => updateTextSizeRef.current?.(), [children]); | ||
|
||
useImperativeHandle(onUpdateRef, () => onUpdate) |
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.
This usage feels a bit magic. Perhaps we should move the responsibility of memorizing the onUpdate function to the user of the hook instead? I mean they'd pass useCallback(f, [])
instead of f
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.
i'm okay with letting the user wrap the callback on an useCallback
, i just used a ref to keep the callback updated because a lot of people don't have the habit to use it, but maybe we can just add a warning in the README and pass this responsibility to the user
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.
I see, perhaps your approach would be good. I'm not familiar with the use of useImperativeHandle in this way. I would have expected:
const onUpdateRef = useRef()
useEffect(() => {
onUpdateRef.current = onUpdateRef
}, [onUpdateRef])
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.
yeah, both the useEffect
and useImperativeHandle
approaches do the same in this case, but I can change to useEffect
if you want, no problem
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.
hey @ViktorQvarfordt, sorry for the huge delay, I've just updated the branch with the origin one, let me know if you still want me to change the useImperativeHandle
call.
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.
Yeah, most people aren't familiar with this hook. Simple is good!
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.
done!
onUpdate
prop added onAutoTextSize
React componentonUpdate
option added onautoTextSize
vanilla methodyalc
added ondevDependencies
Screencast.from.30-11-2023.17.22.12.webm