简体   繁体   English

Reactjs - map function 内的多个 if 条件

[英]Reactjs - Multiple if conditions inside map function

I am trying to add a progress bar with different colors levels at different values.我正在尝试添加具有不同值的不同 colors 级别的进度条。 I have 4 different value option.我有 4 个不同的价值选项。 I am trying to use if/ else if conditions for a particular case but not working.我正在尝试对特定情况使用 if/else if 条件但不起作用。 Is there any better approach.有没有更好的办法。

<ul className="venue-list">
      {searchVenues.map(item => (
        <li className="venue-list__item" key={item.venue.id}>
          <Link
            to={`/venues/${item.venue.id}`}
            className="venue-list__itemLink"
          >
            <div className="venue-list__cover" />
            <img
              src={`${item.venue.photos.groups[0].items[0].prefix}128${item.venue.photos.groups[0].items[0].suffix}`}
              alt="Venue Best Img"
              className="venue-list__image"
            />
            <div className="venue-list__onTopData">
              <h3 className="venue-list__venue-name">{item.venue.name}</h3>
              <div className="venue-list__venueInfo">
                <div className="venue-list__userWrapper">
                  <div className="left">
                    <img src="/image/user-icon.png" alt="Icon" />
                  </div>
                  <div className="right">
                    <span className="user-text">{item.venue.stats.tipCount}</span>
                  </div>
                </div>
                <div className="venue-list__tagWrapper">
                  <div className="left">
                    <img src="/image/tag-icon.png" alt="Icon" />
                  </div>
                  <div className="right">
                    <div className="bar">
                    {
                      if(item.venue.price.tier === 1){
                        return <span className="percentage one"></span>
                      } else if(item.venue.price.tier === 2) {
                        return <span className="percentage one"></span>
                                <span className="percentage two"></span>
                      } else if(item.venue.price.tier === 3) {
                        return <span className="percentage one"></span>
                                <span className="percentage two"></span>
                                <span className="percentage three"></span>
                      } else if (item.venue.price.tier === 4) {
                        return  <span className="percentage one"></span>
                          <span className="percentage two"></span>
                          <span className="percentage three"></span>
                          <span className="percentage four"></span>
                      }
                    }

                    </div>
                  </div>
                </div>
                <div className="venue-list__ratingWrapper">
                  <img src="/image/triangle.png" alt="icon" />
                  <span className="rating-text">{item.venue.rating}</span>
                </div>
              </div>
            </div>
          </Link>
        </li>
      ))}
    </ul>

Now I am getting an error "Syntax error: Unexpected toke for if statement".现在我收到一个错误“语法错误:if 语句的意外标记”。 Whats the best approach to handle cases in map function.在 map function 中处理案例的最佳方法是什么?

在此处输入图像描述

You can use ternary operator 您可以使用三元运算符

<div className="bar">
                    {
                      item.venue.price.tier === 1?
                     <div> <span className="percentage one"></span></div>
                      :item.venue.price.tier === 2?
                       <div> <span className="percentage one"></span>
                              <span className="percentage two"></div></span>
                      :item.venue.price.tier === 3 ?
                        <div> <span className="percentage one"></span>
                                <span className="percentage two"></span>
                                <span className="percentage three"> 
                              </span></div>
                     :item.venue.price.tier === 4?
                        <div>  <span className="percentage one"></span>
                          <span className="percentage two"></span>
                          <span className="percentage three"></span>
                          <span className="percentage four"></span> </div>
                    :''

                }
                    </div>
{item.venue.price.tier === 1 && <span className="percentage one"></span>}

If/else statements don't work in JSX, because of how the 'language' is designed to make calls / construct objects. if / else语句在JSX中不起作用,因为“语言”是如何设计来进行调用/构造对象的。 This is how to achieve conditional rendering. 这是实现条件渲染的方法。

Bear in mind also that you will also have to return just one element. 还请记住,您还必须只返回一个元素。 So you will have to group the latter options like so: 因此,您必须将后面的选项分组,如下所示:

<div>
    <span className="percentage one"></span>
    <span className="percentage two"></span>
    <span className="percentage three"></span>
    <span className="percentage four"></span>
</div>

The problem can be solved by extracting the section to a pure function that can return the set of <span> as an array. 可以通过将节提取为纯函数来解决该问题,该函数可以将<span>作为数组返回。 Here is a way to refactor your code: 这是一种重构代码的方法:

const getPriceTier = (tier = 0) => {
    if (tier > 4) {
        console.log('Config only supports till "percentage four"');
    }

    const config = {
        0: 'one',
        1: 'two',
        2: 'three',
        3: 'four'
    };
    const items = Array.from({ length: tier }, (v, i) => i);
    // items = [0, 1, 2, tier - 1];

    return items.map((i) => {
        return <span className={`percentage ${config[i]}`}></span>
    })
}

And then in the render just use it as: 然后在渲染器中将其用作:

<div className="bar">{getPriceTier(item.venue.price.tier)}</div>

Hope this helps! 希望这可以帮助!

To simplify your example you could use something like 为了简化您的示例,您可以使用类似

const Test = () => {
  const arr = [1,2,3,4]
  return (
    <div>
      {arr.map(e => {
        return (
          (e > 1) ?
          <p key={e}>this is true {e}</p>
          :
          <p key={e}>this is false {-e}</p>
        )
      })}
    </div>
  )
}

{dummyData.map((item) => { if (item.questionType === "singleselect") { return ( <> </> ); } else if (item.questionType === "multiselect") { return ( <> </> ); } return ( <> </> ); })} {dummyData.map((item) => { if (item.questionType === "singleselect") { return ( <> </>); } else if (item.questionType === "multiselect") { return ( <> </> ); } 返回 ( <> </> ); })}

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

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