reactjs - React Router:通过按钮更改组件和路由
问题描述
在 React 应用程序中,有一个带有 2 个按钮的页面。每个按钮呈现不同的组件,但是,路由/URL 不会改变。我怎样才能改变路线,只改变灰色区域?如果用户直接访问 URL,我该如何更改状态?
https://codesandbox.io/embed/elastic-fog-7hqk9?fontsize=14&hidenavigation=1&theme=dark
解决方案
是的,您可以使用Switch
,如果它与 URL 匹配,它将呈现您的组件。这是一个例子:
<Container>
<Header /> // this will render in all cases (Fixed here for example)
<Switch>
<Route exact path="/component1" component={Component1} />
<Route exact path="/component2" component={Component2} />
</Switch>
</Container>
推荐阅读
- clojure - Clojure - 计算地图矢量的累积和
- javascript - 类型上不存在属性“名称”
- r - predict.Arima(object, n.ahead = h) 中的错误:“xreg”和“newxreg”具有不同的列数
- java - org.postgresql.util.PSQLException - 2019
- angular - 如何在 Angular 中“扩展”和包装自定义组件或原生元素?(带有反应示例)
- python - Keras 的 model.predict 可以返回字典吗?
- three.js - 在 three.js / proctree.js 中正确缩放纹理
- docker - Docker 容器在 docker compose 中无法看到彼此,主机也无法看到
- azure - Azure 服务总线主题订阅 REST API
- automated-tests - JSON 字符串中的 Loadrunner 参数