-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
ReactIs.typeOf for non-elements #12882
Comments
Hey @mridgway, thanks for bringing this up. As I understand it, the intention with Changing It sounds like what you want is a hybrid of the two that tells you the type for element types. React.elementType('div')
// Symbol(react.element)
React.elementType(React.StrictMode)
// Symbol(react.strict_mode)
React.elementType(React.forwardRef(Component))
// Symbol(react.forward_ref) Do you think an API like that would suffice for your use case(s)? @bvaughn what do you think? |
That's correct. I can see the usefulness of a method like the one @aweary describes. I don't really have strong thoughts about it other than sharing Brandon's concern about it being a backwards breaking change to make to the existing |
@aweary Yes, I think that API would be sufficient for my use case. Specifically I would be doing something like |
ReactIs.isForwardRef(
React.createElement(
YourComponent
)
) 🤡 |
Yeah, that's what I will do for now. It seems a bit roundabout though and also introduces a dependency on Edit: Just realized that was a clown icon. It did not show up on my Mac 😞 |
I took a stab at implementing |
Checking in on this again. Since it does not seem like there is movement on adding an API to be able to check types without creating elements, I will be implementing my changes using This brings up the question of what the compatibility for |
Should support versions 0.14+ It relies on |
Looks like using |
For future inspiration / real-life use case / other running into this as well: In the mobx-react bindings some special forward ref handling was required as well, but const ReactForwardRefSymbol =
typeof forwardRef === "function" && forwardRef((_props, _ref) => {})["$$typeof"] and: if (ReactForwardRefSymbol && componentClass["$$typeof"] === ReactForwardRefSymbol) {
// something specific
} Doesn't feel entirely kosher, especially the dependency on (N.B.: there is an interesting underlying reason for the special case handling of refs, the bindings are "unpacking" the forward ref to make sure |
A solution to the specific use case of @mridgway would be to make those properties non-configurable and non-writeable. It would help a lot if this would also be considered for future elements (React.memo has already some issues with hoisting until #14313 gets merged). However this should be done in prod mode too to work with |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution. |
Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you! |
What is the current behavior?
ReactIs.typeOf
currently only works for element types, it does not allow you to pass a rawComponent
orForwardRef
to know the type.The use case for this is in
hoist-non-react-statics
I now need a special cases forForwardRefs
. To do this, I will need to know the type of the source and target components, but currently I would need to turn them into elements first.All of the
ReactIs.is*
functions also have this issue since they use thetypeOf
function internally.What is the expected behavior?
Ideally I could pass in just the Component and get the type of it:
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
[email protected]
The text was updated successfully, but these errors were encountered: