-
Notifications
You must be signed in to change notification settings - Fork 40
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
feat: useOverlay #127
base: main
Are you sure you want to change the base?
feat: useOverlay #127
Conversation
- 제어 컴포넌트 사용 케이스에 대응
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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.
First of all, thank you for your contribution.
You asked why the Toss team did not adopt this approach, and there are likely a few reasons:
- From the perspective of closures, it is natural that an overlay component does not track external state changes.
- There have been very few cases where this need arose, and in most cases, the issue could be resolved by allowing overlay components to function in an uncontrolled state.
For these reasons, detecting external state changes was not a priority for overlay components.
However, while it wasn’t a priority, it would certainly be beneficial if an overlay component could detect external state changes.
I have reviewed the PR you worked on.
It appears to function by using a hook to receive the context, detecting changes in the context via useEffect, updating the overlay state accordingly, and then exposing it back through a callback.
If the context contains a primitive value, this wouldn’t be an issue. However, if it holds an object or an array, there could be a problem where useEffect keeps triggering continuously.
Additionally, since there is no way to distinguish whether a value is a getter or a setter, if you process and pass a function instead of directly passing setState, the same issue may occur.
It would be great to find an effective way to resolve this issue.
I also have an additional question.
Since the overlay options type has been updated, it seems that the second argument (context) can now be passed even when calling overlay.open directly, without using useOverlay.
However, due to JavaScript’s nature, in this case, even if context.setValue is called, it seems that the change would not be recognized.
Was this intentional, or was it an unintended addition?
Thank you for your review, and also for sharing why the Toss team did not adopt this approach. Perhaps I need to adapt to using overlay-kit only in an uncontrolled state manner. Here is my response to the review. Passing a reference value inside the context does not cause useEffect to be continuously triggered. No matter what value is passed, the state always flows from OverlayProvider downwards, so it should be referring to the same reference value. In fact, in the example code, the context being passed to the useOverlay hook is also a reference value. Are there any edge cases I might not have considered? Additionally, I believe it is up to the user to determine whether a value is a getter, a setter, or a function used inside an effect (i.e., whether its reference should remain stable). They can also use useCallback or useMemo for values passed to the overlay if needed. Fundamentally, the considerations when passing values into React Context remain the same in this case. The second argument (context) of overlay.open was an unintended addition. To make meaningful use of that option without modifying the code too much, one possible approach would be to rename the variable to initialContext and use it similarly to setting a default value in an uncontrolled component. |
Hello, I read your response carefully. The issue I mentioned about const [data, setData] = useState({
name: 'test',
age: 0,
profile: {
avatar: 'https://randomuser.me/api/portraits/men/1.jpg',
bio: 'Hello!',
joinDate: '2023-05-15',
},
});
const overlay = useOverlay({
context: { profile: data.profile },
});
...
// Outside the overlay component
<button onClick={() => setData({ ...data, age: data.age - 1 })}>-</button> If the state is structured as an object, and only a part of the object is passed as Since As you mentioned, one approach is to shift this responsibility from The issue I brought up can be avoided if developers properly use You used the That said, I completely understand the need to manage overlays in a controlled state, Until then, would it be okay to postpone the review of this PR? |
Yes, of course. I understand what you're saying. I look forward to a better API design. |
Description
Use
useOverlay
to handle dynamic props and make the overlay behave like a controlled component.Changes
Motivation and Context
When I first used overlay-kit, I struggled because the state wasn't reflected as expected. I later realized that overlay-kit doesn't directly provide a way to bind dynamic states. This made me wonder if I could contribute to improving it.
How Has This Been Tested?
Screenshots (if appropriate):
Types of changes
Checklist
Further Comments
I was wondering if Toss team had considered this approach but decided not to provide it for some reason (such as bugs, side effects, usability issues, etc.). If so, I’d love to understand the reasoning behind that decision.