reactjs - 如何在 React 中隐藏/查看组件
问题描述
我有几个组件。我想要做的是当我渲染组件“购物车”时,我想禁用“应用程序”的渲染。
这是我的 index.js
ReactDOM.render(
<Provider store={store}>
<HashRouter>
<App>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/categories" component={Categories} />
<Route path="/category/:categId" component={Products} />
<Route path="/product/:productId" component={Product} />
<Route path="/cart" component={Cart} />
</Switch>
</App>
</HashRouter>
</Provider>
, document.getElementById('root'),
);
我想每次在 App 组件中访问 url 并且每当路径等于“购物车”时我返回 null 但它对我不起作用。
解决方案
使用一个布尔变量和一个三元语句,就像这个超级简化的例子:
const isCart = cartShowing === true
return isCart
? <Cart/>
: <App />
推荐阅读
- sql - 如何按 GROUP 更新具有连续值的列?
- java - Java Web 爬虫和爬虫
- hadoop - 运行命令“hbase 类路径”时出错
- c++ - 在 sdl 中使用不同的 alpha 值绘图
- python - 我想使用 Cognito 的“子”值通过 lambda 触发器创建一个 Dynamodb 条目
- asp.net-mvc - openpgp.initWorker 不是函数
- html - 如何添加 100% 宽度和位置的按钮:固定
- node.js - 当文件路径具有带时间戳的文件夹时,fs.writeFile 不起作用
- r - 如何使用 RMongo 包将日期从 R 存储到 MongoDB?
- python - Anaconda Navigator:无法管理频道