我正在尝试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
但我仍然遇到相同的错误。