-
Notifications
You must be signed in to change notification settings - Fork 801
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
Element type comparing #304
Comments
Hi @Mistereo This is intended, react-hot-loader@3 patches Do you have any need for |
@nfcampos I'm trying to implement conditional rendering depending on children types. |
@Mistereo no, I don't think it can be avoided with the current architecture |
I'm running into the same problem, this is how I am working around the issue: const ImportedComponent = (props) => { ... }
const ImportedComponentType = (<ImportedComponent />).type;
const element = <ImportedComponent />
console.log(element.type === ImportedComponentType) |
this is definitely a big flaw ! not able to check child type of which class ... |
Is it possible to fix this thing or is it too big of a change? |
Ran into this today. One option is to check using
However, even this would also break once the resulting code was minified. So for now I'm using @JorgenEvens 's workaround, thanks for that. |
I'm using another approach
|
Just ran into this myself and I'm pretty stumped. I'd love to do something a little more elegant than checking |
@gaearon Just want to check beforehand that is it possible to fix this in a systematical way or not? If yes, I'll try to dig into it and fix. Thanks. |
also posted on react-proxy gaearon/react-proxy#80 |
+1 const Component = (props) => { ... }
Component.type = (<Component />).type
...
Children.map(children, child => {
if (child.type === Component.type) {
...
}
}) |
Based on what's said here, I've build a very little Babel plugin which transforms Save it as a .js file and reference it in your babel options in dev mode.
|
@lgra - it will not always work, and even can break some generic code. The problem is that React-Hot-Loader will always Currently, there are 4 options:
The best way, I can imagine - we can expose special function for comparison, but how to provide a requirement to use it? |
@theKashey hi Anton. My point of vue is to be less intrusive as possible. At my company, several projects share dozen of UI components. Every projects are not using the same version of REACT, webpack, Babel or even ReactHotLoader. The type comparison is often used in our components, and I don’t want to modify this share code as I can’t quantify the risk, nor the impact on production code - where this modification is not necessary. |
@igra - can you show the rest of your babel plugin? To be more clear - element.type === ImportedComponent
// will be transpiled into
compareRHL(element.type, ImportedComponent)
//where
compareRHL = (a,B) =>{try{ return a===B || (typeof React !== 'undefined' && a===<B/>.type)} catch(e){} return false; } Key points -> keep the old comparison, and there might be no React, and long something.type === something is quite generic. |
@theKashey - There's nothing else ! You've got here the whole code of the plugin. It doesn't produce final transpiled code. It produces JSX code that will be transformed by babel
In the Abstract Syntax Tree, the plugin matchs strict egality expressions (
This is the same AST tree as if you write your code the @JorgenEvens way. The limit of this plugin is that it's assuming that the right identifier is a REACT class identifier. If you don't use |
But is far not easy to transpile something in node_modules and get all the things working properly. |
No, transpiling node_modules is definitely not the way this should be solved (if it is solvable at all). |
@gaearon - could React help? Expose an underlaying type from a proxy as .type prop? In the same time I am just wondering - why one need this comparison outside the tests. What are you going to solve, to archive? I mean - should the case be solved at all. |
@theKashey Any workaround suggestions for monorepos? |
@pitops - v5 would solve this issue next month. |
@theKashey thanks I will probably wait for v5 then |
Hi, |
Same issue as well. |
That's implementation/design/language issue. For now for comparison you may use:
|
Type comparison works fine in production, but fails in dev mode with hot module replacement (HMR) enabled. This is documented in detail at: gaearon/react-hot-loader#304
The @JorgenEvens solution worked pretty well for me, although I did just a minor improvement: const ImportedComponent = (props) => { ... }
const element = <ImportedComponent />
console.log(element.type === ImportedComponent) The type of the instantiated component is basically a reference to the component itself, so the above approach works fine. |
It would print "false". That's the problem. |
v4.10.0(beta) with a patch to FIX THIS ISSUE Just a two years later. Give it a try. |
A patch to fix this issue gaearon/react-hot-loader#304
I'm still seeing this with the latest code... currentSubmit.type === ConfirmationDialog always resolves to false |
@RobertGary1 - works only with |
It seems like with react-hot-loader@3 it's not possible to check element type for imported components:
Logs
false
for me.Is this a bug or something that should not be supported?
The text was updated successfully, but these errors were encountered: