简体   繁体   English

map function 内的条件渲染

[英]Conditional rendering inside a map function

I have a list of buttons in a array我有一个数组中的按钮列表

  const menuItems = [
      {
        key: "delete",
        title: "Delete Order",
        showFor: "all",
      },
      {
        key: "cancel",
        title: "Cancel Order",

        showFor: "all",
      },
      {
        key: "confirm",
        title: "Confirm Order",

        showFor: "unconfirmed",
      },
    ];

Now what i want to render it conditionally,for example i will check if the current state of the component and then render it accordingly.现在我想有条件地渲染它,例如我将检查组件的当前 state 是否然后相应地渲染它。 For example in confirmed order want to show only the delete and cancel not the confirm one.例如,在确认订单中只想显示删除和取消而不是确认。 But for unconfirmed order i want to show all them of them.但是对于未确认的订单,我想展示所有这些。

Currently i am doing like this目前我正在这样做

   {currentTable === "notconfirmed"
          ? menuItems.map((item) => {
              return (
                <Menu.Item key={item.key}>
                    <div>{item.title}</div>
                  </div>
                </Menu.Item>
              );
            })
          : menuItems
              .filter((item) => item.showFor != "unconfirmed")
              .map((kk) => {
                return (
                  <Menu.Item key={kk.key}>
                    <div>
                      <div>{kk.title}</div>
                    </div>
                  </Menu.Item>
                );
              })}

It is working fine but this looks really messy any clean way of doing this?它工作正常,但这看起来真的很乱,有什么干净的方法吗?

Well that is the way it done.嗯,它就是这样做的。

You can refactor your code moving the render logic of each block into a function or a component to make it more clean.您可以重构代码,将每个块的渲染逻辑移动到 function 或组件中,以使其更干净。

You can add conđition in map like this:您可以像这样在 map 中添加条件:

{menuItems.map((item) => {
      if (
        currentTable !== "notconfirmed" &&
        item.showFor === "unconfirmed"
      ) {
        return null;
      }
      return (
        <Menu.Item key={item.key}>
          <div>{item.title}</div>
        </Menu.Item>
      );
    })}

I think that you need to refact the data... It's not a good idea to have the filter logic in the data... The property showFor seems like Code Smell我认为您需要重构数据...在数据中包含过滤器逻辑不是一个好主意...属性showFor看起来像Code Smell

But there is an example to improve your code.但是有一个例子可以改进你的代码。

If you wanna keep your filter function, you use something like:如果你想保留你的filter function,你可以使用类似的东西:

const component = (menuItems, currentTable = "notconfirmed") => {
    const filterValue = currentTable === "notconfirmed" 
    
    return <>
        {menuItems.filter((item) => filterValue ? item.showFor === "unconfirmed" : item.showFor !== "unconfirmed").map((filterdItem) => (
            <Menu.Item key={filterdItem.key}>
                <div>
                    <div>{filterdItem.title}</div>
                </div>
            </Menu.Item>
        ))}
    </>
}

In order to avoid the filter to make less operations and optimize your code, it's a better option:为了避免过滤器减少操作并优化代码,这是一个更好的选择:

const component = (menuItems, currentTable = "notconfirmed") => {
    const filterValue = currentTable === "notconfirmed" ? "all" : "unconfirmed"
    
    return <>
        {menuItems.map((filterdItem) =>
        (
            filterdItem === filterValue && <Menu.Item key={filterdItem.key}>
                <div>
                    <div>{filterdItem.title}</div>
                </div>
            </Menu.Item>
        ))}
    </>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM