Description
Steps to reproduce
My intent was to have my application View Yield a react component that is defined in the controller via render component:...
then i would rarely have to interact with rails Views, and react components are now my views.
This works in principle, but it fails under specific cases. Presumably because it renders the component in a different order to the render_component method available in Views as this method does not bug in the same way, and works as expected.
This will bug by giving an error:
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got:
undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and
named imports
When your imports are fine, this was caused in my case by window.screen.availWidth
used above the export default function Component(){}
being used in one of the imported files. This was in my case redundant so i could remove it for this import, but unfortunately i also import Uppy in one of my files and this causes the same error - import { DashboardModal } from '@uppy/react'
(presumably because they do something similar, but i cannot change the source code here.)
This makes it impossible to render my react in this way. Which is annoying because i now have to use the view and it's more convoluted that way. The method react_component
used in views works just fine and does not experience this bug - presumably because at compile time or similar the window object exists as opposed to rendered from the controller where the window has not been instanced, but thats a guess.
So to summarise - Controller method 'render component: ' fails to import certain files due to import error when View method react_component succeeds just fine. Presumably because they load the files at different times.
What should happen - the controller method should load the files at the same time as render_component in order to more robustly load imports. ( or if this is not the cause, the controller method should be brought in line with the view method so as not to fail imports )
Current workaround - just use Views with <%= react_component "Component" %> inside the view instead.
react rails 2.6.1