controller method 'render component' fails to import properly where <%=react_component() %> successfully imports the same files)

Open
#1,126 4 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
32/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, rails, react
Domain
full-stack

Research direction

Start from the controller render component: entry point and compare its loading path with the view react_component method. Reproduce the failure using an import that references window.screen.availWidth or @uppy/react; done when controller rendering handles the same imports successfully as the view method.

Written by the indexing model from the issue text.

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

Dominant language
JavaScript
Stars
6.8k
Forks
739
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from reactjs/react-rails

All issues in reactjs/react-rails

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.