【发布时间】:2020-05-18 09:25:17
【问题描述】:
我正在尝试为我的应用设置react-rails (https://github.com/reactjs/react-rails)。
最初设置它没有问题,但我预计会有很多不同的单独组件,所以我想整齐地组织我的 /javascript/components 文件夹。
所以我有这样的东西
components
character
avatar-selector
AvatarSelector.tsx
HomeLogo.tsx
AvatarSelector.tsx 默认导出组件。
我有
const componentRequireContext = require.context("components", true);
const ReactRailsUJS = require("react_ujs");
ReactRailsUJS.useContext(componentRequireContext);
在packs/application.js 和packs/server-rendering.js 中
当我渲染 HomeLogo 组件时,无论有没有预渲染,一切都很好。
然后,当我使用 react_component('character/avatar-selector/AvatarSelector', avatar_props, prerender: false) 渲染组件时,它也可以正常工作。
但是当我切换到prerender: true 时,它给出了一个错误。
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 character/avatar-selector/AvatarSelector
我已经试过直接加了
const AvatarSelector = require('../components/character/avatar-selector/AvatarSelector')
到我的包并尝试将组件呈现为AvatarSelector,但我仍然遇到同样的错误。
【问题讨论】:
标签: ruby-on-rails reactjs react-rails execjs server-rendering