-
Notifications
You must be signed in to change notification settings - Fork 61
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
More complex examples or good/bad practices documented #8
Comments
Hey, thanks for your feedback. Yeah, more examples in docs would be nice. I was actually hoping the RFC would be discussed actively and I wouldn't need to keep this repo if it were to be a part of React some time soon. So, I didn't prioritize documentation. My current focus is documenting more for React Tracked. (Actually, this repo was born as a simpler version without I'm happy to answer questions. In your example, I think you need to memoize objects. const [listeners, setListeners] = useState([])
const [state, setState] = useState({}) // some other state
const addListener = useCallback((listener) => {
// some complex logic
setListeners(newListeners)
}, [...])
const value = useMemo(() => ({
state,
addListener,
}), [state, addListener])
return (
<Provider value={value}>
{children}
</Provider>
) Finally, @JoviDeCroock have incorporated use-context-selector in his hooked-form, so that might help as a practical example. |
I have converted to my own event emitter for the React warning and performance when using 1000+ useContextSelectors. This also gave some size advantages for me, I’ll answer with how I used it here tomorrow. |
At a glance, it's so tuned and tightly-coupled that it's not like a good example to explain a use case of use-context-selector. 😅 (I wonder if you really need a context value in hooked-form. I mean useSubscription might just work.) |
use-subscription would impose needless bloat and an alternative thinking pattern to the |
I assumed that everything can be hidden from the library users, but I'm not suggesting it anyway. |
What aspects are you interested in in specific @dai-shi I use an event emitter comparable to https://github.com/developit/mitt and the listeners are all registered in a Whenever a value/error/... changes the event is emitted to that one component, so only one component (or multiple if there are more listeners) will rerender. This seems to be concurrent friendly since all the changes to these watched values are invoked in |
a) how hooked-form used to use use-context-selector 👉 a good practice b) how hooked-form works now without it 👉 sort of a bad practice |
A) it used to always use three separate useContextSelectors to get value, errors and touched with a recursive function as the selector. This meant that it only did string/null/undefined comparison resulting in an ideal scenario for So
B) I can't see how this is a "bad" practice since this boosts performance and is concurrent friendly, one of the most popular form libraries
|
Oops, I meant a case that doesn't fit with useContextSelector as is. no? |
Closing this. Please open a new issue for further discussion. |
Hey! Awesome work! I hope it's gonna be a part of React 🤞.
To be precise: I'm not raising issue, rather feature request - could you please extend documentation or add more complex examples? Especially part about good/bad practices. For example how to access function/objects stored in context because those must have same reference in order to skip update.
Example - component is getting function from context:
Provider looks like:
Therefore as compare condition is:
Update will be triggered. However it might be fixed by useRef:
I think there are more examples of how to use/how to not use this hook and it might be helpful to document it.
Cheers!
The text was updated successfully, but these errors were encountered: