Skip to content
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

childrenOf and childrenOfType fail with react-hot-loader #2641

Closed
liviudobrea opened this issue May 3, 2018 · 8 comments
Closed

childrenOf and childrenOfType fail with react-hot-loader #2641

liviudobrea opened this issue May 3, 2018 · 8 comments
Labels
package: react carbon-components-react severity: 2 User cannot complete task, and/or no workaround type: bug 🐛

Comments

@liviudobrea
Copy link

Detailed description

react-hot-loader (v4.x.x) (will further refer to this as RHL) wraps every React component in a ProxyComponent, to internally handle the change / reload mechanism. That is all good, however, by using RHL with carbon-components-react, this statement https://github.com/carbon-design-system/carbon-components-react/blob/706584394a321c30c56367f1fee930a82fb26236/src/prop-types/childrenOf.js#L24-L28 and this statement https://github.com/carbon-design-system/carbon-components-react/blob/706584394a321c30c56367f1fee930a82fb26236/src/prop-types/childrenOfType.js#L20-L24 will always throw something similar to this:

warning.js:33 Warning: Failed prop type: Invalid prop `children` of type `ListBoxField` supplied to `ListBox`, expected each child to be one of: `[ListBoxField, ListBoxMenu]`.

Is this a feature request (new component, new icon), a bug, or a general issue?

This is not necessary an issue with the current code, but it becomes a bug when using this with the RHL 3rd party library (which I'm guessing many are).

This is an acknowledged issue with RHL. More details here gaearon/react-hot-loader#304.

Some Solutions

Depend on react-hot-loader

I'm not sure if this is a good enough reason to add RHL as a project dependency, but it provides the cleanest solution.

import { areComponentsEqual } from 'react-hot-loader';

...

if (
  !expectedChildTypes.includes(child.type) && 
  (
    typeof child.type === 'function' && 
    !areComponentsEqual(child.type, expectedChildTypes.find(c => c.name === childDisplayName)))
  )
)

Don't depend on react-hot-loader

This one's a tad uglier, but it also works. However, since RHL API could change at any given time, this is more likely to fail at some point.

(
  typeof child.type === 'function' &&
  Object.prototype.hasOwnProperty.call(
    child.type,
    '__reactstandin__getCurrent'
  ) && 
  child.type.__reactstandin__getCurrent() !== expectedChildType
)

More than happy to PR if you're ok with either of those.

@joshblack
Copy link
Contributor

Hi @liviudobrea! 👋 I definitely wouldn't mind the first one given that it's the proper solution, in this case. My only concern would be if using that solution could potentially harm consumers who aren't using RHL?

If that's the case, wouldn't mind a general getProxyFor utility that encapsulates that behavior in the second example that you noted. It would also work as a place to hold logic for other libraries that might proxy React component, if that ever occurs.

@liviudobrea
Copy link
Author

@joshblack Will run some intentional mis-usage test cases, with and without RHL, and report back.

@liviudobrea
Copy link
Author

Implementation in #859

liviudobrea referenced this issue in liviudobrea/carbon-components-react Aug 29, 2018
- Remove react-hot-loader dep;
- Add react-proxy to help with testing;
- Add internal helper to check for component type equality in case of
proxied components;
@liviudobrea
Copy link
Author

liviudobrea commented Aug 29, 2018

Another take at it in #1253

liviudobrea referenced this issue in liviudobrea/carbon-components-react Aug 29, 2018
- Remove react-hot-loader dep;
- Add react-proxy to help with testing;
- Add internal helper to check for component type equality in case of
proxied components;
liviudobrea referenced this issue in liviudobrea/carbon-components-react Aug 29, 2018
liviudobrea referenced this issue in liviudobrea/carbon-components-react Aug 30, 2018
- Optimize for prettier;
@ollie-o
Copy link
Contributor

ollie-o commented Oct 25, 2018

@joshblack I noticed that both of the attempted solutions to this issue ended up not getting merged in.

Seems like #1253 in particular was super close to getting merged!

Are there plans for someone from the core carbon-react team to fix it? I get the impression that it could be solved pretty quickly if one of you picks up the issue and take over from where #1253 left things.

@joshblack
Copy link
Contributor

joshblack commented Oct 25, 2018

@oliverodaa most likely we'll loosen our prop type definitions versus trying to fix it 😞 It's a very hard bug to fix which is why most PRs tend to die down.

Definitely feel free to contribute a fix based on 1253, was looking pretty promising 😄

@stale
Copy link

stale bot commented May 1, 2019

We've marked this issue as stale because there hasn't been any activity for a couple of weeks. If there's no further activity on this issue in the next three days then we'll close it. Thanks for your contributions.

@carbon-bot carbon-bot transferred this issue from carbon-design-system/carbon-components-react May 9, 2019
@carbon-bot carbon-bot added package: react carbon-components-react severity: 2 User cannot complete task, and/or no workaround type: bug 🐛 wontfix labels May 9, 2019
@carbon-bot
Copy link
Contributor

Hi there! 👋 If you're wondering why this issue was moved, we're currently updating our repo structure so that every package is found in the same project.

This should not have any impact for you, but we wanted to give you a heads up in case you were wondering what is going on. If you have any questions, feel free to reach out to us on Slack or contact us at: carbon@us.ibm.com. Thanks!

@project-bot project-bot bot added this to Issue: needs triage 💊 in Carbon support May 9, 2019
@dakahn dakahn closed this as completed May 13, 2019
Carbon support automation moved this from Issue: needs triage 💊 to Issue/PR: Closed 🙌 May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: react carbon-components-react severity: 2 User cannot complete task, and/or no workaround type: bug 🐛
Projects
None yet
Development

No branches or pull requests

5 participants