javascript - ReactJs 动态组件渲染与动态路由
问题描述
在我的项目中,根据用户选择的应用程序类型,组件必须渲染并进一步导航到不同的路线。在所有这些应用程序中,布局保持不变。我必须根据路线添加不同的组件。首页主要组件
<Card>
<Card.Body>
<DYNAMIC_COMPONENT /> //based on application chosen
<button onClick={()=> takeToNextRoute}>CLICK ME</button>
<Card.body>
</Card>
基于按钮点击Home
,用户将被带到不同的路线Application type
<div>
<Router>
<Switch>
<Route exact path='/:{based on app}' component={dynamicCOMP}></Route>
<Route exact path='/about:{based on app}' component={dynamicCOMP1}></Route>
<Route exact path='/profile:{based on app}' component={dynamicCOMP2}></Route>
<Route exact path='/select:{based on app}' component={dynamicCOMP3}></Route>
</Switch>
</Router>
</div>
如何Home
根据application
选择的动态渲染组件,然后使用它的button click
路由?我是 ReactJs 的新手,不知道如何去做。dynamic routes
dynamic components
解决方案
您可以拥有一个包含所有路由和组件的对象:
const routes = {
"/": HomeComponent,
"/about": AboutComponent,
"/profile": ProfileComponent,
"/select": SelectComponent
}
然后在你的路由器中,它看起来像:
<div>
<Router>
<Switch>
{Object.keys(routes).map((route, i) => <Route key = {i} exact path = {route} component = {routes[route]}/>
</Switch>
</Router>
</div>
推荐阅读
- git - SSH GitLab pull 总是要求输入密码
- android - Cordova Android 版本构建 Ajax 无法正常工作
- html - Lottie 特定循环帧
- angular - 类型'可观察的
' 不可分配给类型 'never' - python - 将搜索路径添加到 VS Code 工作区
- android - Kotlin 伴随对象 - 初始化块 - 类型别名
- python - 尝试/排除“缓存”请求
- javascript - 为什么我的客户端代码在节点后端编译和运行?
- vb.net - ufl dll 文件未在水晶报表设计器中显示,它采用双精度、字符串、整数类型的 dll,但位图、图像、对象不采用它
- git - Git CA 失败但证书有效(gitea)