首页 > 解决方案 > 如何将具有条件的两个组件传递给单个路由() 反应 jsx 中的方法?

问题描述

(将带有条件的两个组件传递给反应jsx中的单个路由()方法?

.

标签: reactjs

解决方案


如果我正确理解你的问题,你只需要添加一个三元表达式。如果条件为真,您将渲染 Component1。如果为 false,您将渲染 Component2。

<Route path="/your_pathname_here">
    {condition ? <Component1 /> : <Component2 />}
</Route>

我在这里假设您使用的是 react-router。目前尚不清楚您使用的是什么路由,因为您没有在问题中添加该信息。


推荐阅读