简体   繁体   English

如何使用 react 和 javascript 在某些条件为真时显示组件?

[英]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.

相关问题 当某个条件为真时如何使用 javascript 重定向到另一个页面 - how to redirect to another page when a certain condition is true using javascript 如何在 ReactJS 中某些条件为真时显示组件? - How to display a component when certain conditions are true in ReactJS? 当道具检查为真时,如何在类组件中使用 react-toastify 显示吐司 - How to display toast using react-toastify inside class component when props check is true 如何使用 react 和 javascript 根据可重用组件中的条件显示特定文本? - How to display a particular text based on a condition in a reusable component using react and javascript? 如何仅在使用反应和 typescript 的条件为真时呈现 jsx? - How to render jsx only when a condition is true using react and typescript? 当组件的某个状态为true时,如何访问react native中加载的元素? - How to access an element in react native which is loaded when a certain state of the component is true? 如何使 API map 标记出现在某个条件为真时 - How to make API map markers appear when a certain condition is true 如果在View中如果条件为true而不使用Onclick等,如何显示模式弹出窗口? 使用mvc .net - How to display modal popup when the if condition is true in View without using Onclick etc? using mvc .net 只有在某个条件为真后才在 React 中返回? - Only return in React after a certain condition is true? 如何根据 React Spring 的条件结果显示和动画 React 组件? - How to display and animate React Component depending to result of condition with React Spring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM