简体   繁体   English

在 React 中,如果 state 是一个空数组,那么如何有条件地呈现文本?

[英]In React, if a state is an empty array then how do you render a text conditionally?

I want to render 'menu is empty'.我想渲染“菜单为空”。 Otherwise I am able to map through and render the json data from the api correctly.否则,我可以通过 map 并正确渲染来自 api 的 json 数据。

const Navbar = () => {
    const [menu, setMenu] = useState([]);

    if (menu === []) {
        setMenu(["menu is empty"])
    }

    return (
        <div className='nav-area'>
        <Link to='/' className='logo'>LOGO</Link>
            <ul className='menus'>
            {menu.map((main) => (
            <MenuItems items={main} key={main.type}/>
            ))}
            </ul>
        </div>
    );
};

export default Navbar;

you can simply check your array length and base on that render text or ul with items您可以简单地检查您的数组长度并基于该渲染文本或带有项目的 ul

sth like this:像这样的某事:

const Navbar = () => {
    const [menu, setMenu] = useState([]);


    return (
        <div className='nav-area'>
        <Link to='/' className='logo'>LOGO</Link>
        {
              menu.length > 0 ? (
                <ul className='menus'>
  
           {
              menu.map((main) => (
                <MenuItems items={main} key={main.type}/>
                )) 
            }
           
            </ul>
              )  : <>menu is empty</>
          }
           
        </div>
    );
};

export default Navbar;

You can use inline If-Else to render conditionally.您可以使用内联 If-Else 有条件地呈现。

Inline If-Else with Conditional Operator 带有条件运算符的内联 If-Else

Example:例子:

const Navbar = () => {
    const [menu, setMenu] = useState([]);

    if (menu === []) {
        setMenu(["menu is empty"])
    }

    return (
        <div className='nav-area'>
        <Link to='/' className='logo'>LOGO</Link>
            <ul className='menus'>
            { menu.lenght < 1
                  ? menu.map((main) => (
                        <MenuItems items={main} key={main.type}/>
                    ))
                  : 'menu is empty'
             }
            </ul>
        </div>
    );
};

export default Navbar;

In this scenario there is two possibilities either menu is empty or have some data.在这种情况下,有两种可能性菜单为空或有一些数据。

What i do in this situation is check for condition whether array is empty by .length property and also check for if not empty then is it an array or not by Array.isArray method.在这种情况下,我所做的是通过.length属性检查数组是否为空,并通过Array.isArray方法检查是否为空,然后检查它是否为数组。

If both condition satisfy then render the content otherwise render the "Menu is empty".如果两个条件都满足,则呈现内容,否则呈现“菜单为空”。 Something like this像这样的东西

const Navbar = () => {
  const [menu, setMenu] = useState([]);

  return (
    <div className="nav-area">
      <Link to="/" className="logo">
        LOGO
      </Link>

      {Array.isArray(menu) && menu.length ? (
        <ul className="menus">
          {menu.map((main) => (
            <MenuItem items={main} key={main.type} />
          ))}
        </ul>
      ) : (
       // or you may have a component for this 
        <h3>Menu is Empty</h3>
      )}
    </div>
  );
};

export default Navbar;

right way using react使用反应的正确方法

import { useMemo } from "react";

const Navbar = () => {
    const [menu, setMenu] = useState([]);

    const displayMenus = useMemo(
        () =>
        {
            if(menu.length === 0) return <div>EMPTY</div>
            return menu.map(
                (main) =>
                {
                    return <MenuItems items={main} key={main.type}/>
                }
            )
        },
        [menu]
    )

    return (
        <div className='nav-area'>
            <Link to='/' className='logo'>LOGO</Link>
            <ul className='menus'>
                {displayMenus}
            </ul>
        </div>
    );
};

export default Navbar;

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

相关问题 如何根据数组的内容有条件地渲染 React Native FlatList? - How do you conditionally render a React Native FlatList depending on the contents of the array? 你如何在反应中清空数组? - How do you empty an array in react? 如何根据来自 api 的数据是否可用有条件地在 React js 中呈现按钮? - How do you conditionally render a button in React js based on if data from api is avaliable or not? 反应:如何有条件地渲染? - React: How to conditionally render? 如何在 React 中的对象数组上 map,然后根据先前的值有条件地渲染组件? - How do I map over an array of objects in React and then conditionally render a component based on a previous value? 如何将 arrays 插入嵌套数组的 state 变量中? - 反应 - How do you insert arrays into a state variable of a nested array? - React 在 JSX 文本中有条件地呈现日期 state 变量 - Conditionally render date state variable in JSX Text 使用 React &amp; Bootstrap 有条件地渲染空 div 或错误 - Conditionally render empty div or error with React & Bootstrap React JS state 数组在 function 内为空,但项目出现在渲染中 - React JS state array empty inside function but items appear in render 我如何有条件地在反应中呈现字体真棒图标 - How do I conditionally render font awesome icons in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM