-
Notifications
You must be signed in to change notification settings - Fork 104
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
Support for forwardRef
components
#187
Comments
It looks like Mobx's I don't know if that can be avoided, and make it "just work". When using an external library like Emotion, people is not aware that the components created by the library are not regular components but forwardRef components. |
This is a pretty valid bug, thanks 🙂. I will look into it for the v7 release. |
Awesome! Thanks @solkimicreb 😄 |
I've studied this a little bit. It looks like it's easy to detect when a component is a forwardRef using: baseComponent["$$typeof"] === Symbol.for("react.forward_ref"); I don't know at which version those symbols where introduced because the Mobx library has a fallback to creating a forwardRef component on the fly to extract it when it's not available. They claimed they had some problems with Then, the render is on the baseComponent["render"]; It's interesting to read both the
The if (baseComponent["$$typeof"] === Symbol.for("react.forward_ref"))
return React.forwardRef((args) => (
<Observer>{() => baseComponent["render"].apply(undefined, args)}</Observer>
)); I don't quite understand why they use On the other hand, the const memoComponent = memo(forwardRef(wrappedComponent)); instead of just const memoComponent = memo(wrappedComponent); In const wrappedComponent = (props, ref) => {
return useObserver(() => baseComponent(props, ref));
}; I don't quite get why they are doing all that instead of just going with a straightforward approach:
|
have there been any progress concerning this? |
Is your feature request related to a problem? Please describe.
Right now with
react-easy-state
we can make any functional component or class component reactive, but there's a third type of component calledforwardRef
that is not supported.Describe the solution you'd like
Even though this
forwardRef
components are not very common and mostly used by library authors, I think it could be great to have the ability to make them reactive as well.Describe alternatives you've considered or seen elsewhere
I think the main alternative is to wrap this components inside an additional normal component that is reactive, extract the props that the forwardRef component needs, pass them down and then when it rerenders, rerenders the forwardRef component children as well.
Additional context
This
forwardRef
pattern is common on style libraries likestyled-components
oremotion
. For example, this is the implementation ofstyled
in Emotion: https://github.com/emotion-js/emotion/blob/6cb8d15438b01b8623af42f304d5ea3032332187/packages/primitives-core/src/styled.js#L33The React docs for this are here: https://reactjs.org/docs/forwarding-refs.html
I have made a codesandbox with an example of the issue and the forwardRef components: https://codesandbox.io/s/res-and-forwardref-components-74mvm?file=/src/App.js
The text was updated successfully, but these errors were encountered: