我一直在尝试模块化我的 React.js 应用程序(它将作为带有 Electron 的桌面应用程序交付),如果我将来创建一个新模块,我可以只添加一个新文件夹并修改几个文件,它应该可以很好地集成。 我最初的灵感来自这篇文章:https://www.nylas.com/blog/react-plugins/
在那之后,我开始尽可能多地进行研究,并最终创建了一个 JSON 文件,该文件将存在于服务器中,其中包含为该特定客户端注册的插件 list 。 像这样:
{
"plugins": [
{
"name": "Test Plugin",
"version": "0.0.1",
"path": "testplugin",
"file": "test",
"component":"TestPlugin"
},
{
"name": "Another Plugin",
"version": "0.0.1",
"path": "anothertest",
"file": "othertest",
"component":"TestPluginDeux"
}
]
}
之后,我创建了几个匹配 path
值的文件夹,其中包含一个与 list 中的名称匹配的组件(例如,默认导出 testplugin/test.jsx
组件的 TestPlugin
)。我还制作了一个 pluginStore
文件,用于读取 list 并将插件安装在 this.state
中。
然后,在 Google 和此处进行了大量研究,找到了这个答案:React - Dynamically Import Components
通过该功能,我能够遍历 list ,找到目录中的文件夹,并通过运行我在主页的 this.state
方法中创建的 mountPlugins()
函数将插件安装到 pluginStore
中。
到目前为止一切顺利。我正在使用 React-Router,我能够在 State 中动态安装插件,并能够通过像这样调用它们将它们加载到我的 Home Route 中:componentDidMount()
。
我现在遇到的问题是,我想通过使用 <TestPlugin />
或 component
方法动态创建从状态加载这些组件的路由,但我没有运气。我总是会得到相同的结果……显然我传递的是对象而不是字符串。
这是我这次尝试的最后一次迭代:
{this.state.modules.registered.map((item) =>
<Route exact path={`/${item.path}`} render={function() {
return <item.component />
}}></Route>
)}
之后,我创建了一个调用 render
组件的 Route,该组件由一个 PluginShell
调用,该 Navlink
发送插件的名称以动态注入(inject)和加载它。
<Route exact path='/ex/:component' component={PluginShell}></Route>
但我最终遇到了完全相同的问题。我正在传递一个 object
并且 createElement
函数需要一个 string
。
我搜索了整个 StackOverflow,发现了很多类似的问题和答案。我尝试应用所有可能的解决方案,但没有成功。
编辑: 我整理了一个 GitHub 存储库,其中包含可重现该问题的最少文件集。
最佳答案
好吧。这里有很多事件部件可以大大简化。
- 我建议转向对开发人员更友好、固执己见的状态存储(例如 Redux )。我个人从未使用过 Flux,所以我只能推荐我使用过的东西。因此,您可以避免使用普通类进行状态管理。
- 您应该只在初始应用程序加载期间导入模块一次,然后您可以分派(dispatch)一个操作将它们存储到 (Redux) 状态,然后根据需要与组件共享状态(只有在共享状态时才需要许多组件分布在您的 DOM 树中,否则根本不需要)。
- 模块导入是异步的,因此不能立即加载。在将模块映射到
Route
之前,您必须设置一个条件来等待加载模块(在您的情况下,您试图将模块的注册字符串名称映射到路由,而不是导入模块功能)。 - 理想情况下,模块导入应该包含在状态中的已注册模块中。换句话说,当您导入模块时,它应该只是用组件函数覆盖模块组件字符串。这样一来,所有相关信息都放在一个对象中。
- 无需将模板文字与字符串连接混合搭配。使用其中之一。
- 使用setState callback在覆盖之前传播任何
previousState
。看起来更简单、更干净。 - 将您的 import 语句包装在
try/catch
block 中,否则,如果该模块不存在,它可能会破坏您的应用程序。
工作示例(我只是在这个简单示例中使用 React 状态,我也没有接触任何其他文件,这些文件也可以简化):
App.js
import React from "react";
import Navigation from "./components/MainNavigation";
import Routes from "./routes";
import { plugins } from "./modules/manifest.json";
import "./assets/css/App.css";
class App extends React.Component {
state = {
importedModules: []
};
componentDidMount = () => {
this.importPlugins();
};
importPlugins = () => {
if (plugins) {
try {
const importedModules = [];
const importPromises = plugins.map(plugin =>
import(`./modules/${plugin.path}/${plugin.file}`).then(module => {
importedModules.push({ ...plugin, Component: module.default });
})
);
Promise.all(importPromises).then(() =>
this.setState(prevState => ({
...prevState,
importedModules
}))
);
} catch (err) {
console.error(err.toString());
}
}
};
render = () => (
<div className="App">
<Navigation />
<Routes {...this.state} />
</div>
);
}
export default App;
routes/index.js
import React from "react";
import React from "react";
import isEmpty from "lodash/isEmpty";
import { Switch, Route } from "react-router-dom";
import ProjectForm from "../modules/core/forms/new-project-form";
import NewPostForm from "../modules/core/forms/new-post-form";
import ProjectLoop from "../modules/core/loops/project-loop";
import Home from "../home";
const Routes = ({ importedModules }) => (
<Switch>
<Route exact path="/" component={Home} />
<Route exact path="/projectlist/:filter" component={ProjectLoop} />
<Route exact path="/newproject/:type/:id" component={ProjectForm} />
<Route exact path="/newpost/:type" component={NewPostForm} />
{!isEmpty(importedModules) &&
importedModules.map(({ path, Component }) => (
<Route key={path} exact path={`/${path}`} component={Component} />
))}
</Switch>
);
export default Routes;
关于javascript - 在 React Router 路由中动态注入(inject)数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57513042/