reactjs / react-rails

Integrate React.js with Rails views and controllers, the asset pipeline, or webpacker.
Apache License 2.0
6.74k stars 757 forks source link

prerendering material-ui components #1152

Open sternj opened 2 years ago

sternj commented 2 years ago

I have been attempting to perform server-side rendering of mui (formerly material-ui) components and have been getting issues similar to #193. The precise error I am getting is

ActionView::Template::Error (Encountered error "#<ExecJS::ProgramError: 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 prerendering MaterialComponentList with {}

I was wondering whether the state of the art has changed with regards to using server-side rendering. For the sake of example, I am using a modified form of this in this gist

I'm on rails 6.1.4.4 with Ruby 3.0 and reasct-rails 2.6.1.

If it would be helpful I can spin up and post a minimal example.

trostli commented 2 years ago

I have been seeing this as well. Unfortunately all I have been able to come up with is to avoid server-side rendering for the components that give me this error... I am wondering what the underlying cause is too .

alexisrabcarv commented 9 months ago

I had that issue before, like the error states, you are passing in undefined when your prop expects a string or a function. Check your life cycle and all your variables to see which one get's inside undefined. Prerendering happens before viewing the page so window and document variables are undefined as well.

Also try to use functional components, class components are heavier and it could be causing the pre-render to fail.