[英]How to display a component when certain condition true using react and javascript?
The layout component is rendered on all pages.布局组件在所有页面上呈现。
I want to achieve the following我想实现以下
in /items page
*Layout component is displayed if the user is admin
* Layout component not displayed if the user is non-admin
below is my code,下面是我的代码,
function Main() {
const isAdmin = getUser();
return(
<Switch>
<Route
exact
path="/items"
render={routeProps => (
<Layout>
{isAdmin ? <Items {...routeProps} />: <NotFound/>}
</Layout>
)}
/>
<Route
exact
path="/home"
render={routeProps => (
<Layout>
<Home {...routeProps} />
</Layout>
)}
/>
</Switch>
);
}
const Layout: React.FC = ({ children }) => (
<>
<TopBar />
{children}
<BottomBar />
</>
);
As you see from the above code, the Layout component is displayed in all pages and is used as a wrapper for other routes too like for /home从上面的代码中可以看出,Layout 组件显示在所有页面中,并用作其他路由的包装器,例如 /home
Now I don't want the Layout component to be displayed only in /items page if a user is not admin现在,如果用户不是管理员,我不希望 Layout 组件仅显示在 /items 页面中
What I have tried?我试过什么?
const Layout: React.FC = ({ children }) => {
const history = useHistory();
const isItemsPath = history.location.pathname.includes('/items');
const isAdmin = getUser();
return (
<>
{!isItemsPath && <TopBar />
{children}
{!isItemsPath && <BottomBar />
</>
);
} }
But this will not display TopBar and BottomBar if the items page even if the user is admin.但是,即使用户是管理员,如果项目页面也不会显示 TopBar 和 BottomBar。 how can I modify the condition
如何修改条件
such that TopBar and BottomBar are displayed in all pages except items page if not admin.如果不是管理员,则 TopBar 和 BottomBar 显示在除项目页面之外的所有页面中。
could someone help me with this?有人可以帮我吗? thanks.
谢谢。 };
};
What about you change the condition in Route
?您如何更改
Route
中的条件?
<Route
exact
path="/items"
render={routeProps => (
{isAdmin ? <Layout>
<Items {...routeProps} />
</Layout>
: <NotFound/>}
)}
/>
If I understand correctly, you might be looking for something along the lines of this:如果我理解正确,您可能正在寻找类似以下内容的内容:
const Layout: React.FC = ({ children }) => {
const history = useHistory();
const isItemsPath = history.location.pathname.includes('/items');
const isAdmin = getUser();
return (
<>
{(!isItemsPath || isAdmin) && <TopBar />
{children}
{(!isItemsPath || isAdmin) && <BottomBar />
</>
);
Then you should be able to remove your isAdmin
condition in your Main
component.然后你应该能够在你的
Main
组件中删除你的isAdmin
条件。
In your layout component you can use conditional rendering.在您的布局组件中,您可以使用条件渲染。 We can check if the page is isItemsPath first, if it is items path and user is not admin then we do not show the Topbar and BottomBar, for all other pages we show them
我们可以先检查页面是否是isItemsPath,如果是项目路径并且用户不是管理员,那么我们不显示Topbar和BottomBar,对于所有其他页面我们显示它们
const Layout: React.FC = ({ children }) => {
const history = useHistory();
const isItemsPath = history.location.pathname.includes('/items');
const isAdmin = getUser();
return !(isItemsPath && !isAdmin) ?
<>
{children}
</> : (
<>
<TopBar />
{children}
<BottomBar />
</>
);
}
You have some choice to doing this, but I think the best way is using HOC if you have to repeat checking the user is admin or not, pass your component to HOC and in HOC component check if a user is an admin or not.您可以选择这样做,但我认为最好的方法是使用HOC如果您必须重复检查用户是否为管理员,将您的组件传递给 HOC 并在 HOC 组件中检查用户是否为管理员。 You can use this HOC component for all of your components.
您可以将此 HOC 组件用于您的所有组件。 In HOC component use conditional rendering.
在 HOC 组件中使用条件渲染。 Something like this:
像这样的东西:
function checkAdmin(WrappedComponent, selectData) {
const isAdmin = getUser();
render() {
return ({isAdmin} ? <WrappedComponent /> : <div></div>)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.