简体   繁体   English

为什么反应不条件渲染

[英]why would react not render conditionally

So i have an issue while trying to render this list, item.cars, item.yachts and item.villas are numbers, what could be the reason for this not showing absolutely anything?所以我在尝试渲染这个列表时遇到了一个问题,item.cars、item.yachts 和 item.villas 是数字,这可能是什么原因没有显示任何东西?

const renderItems = (locations) => {
<div className={styles.list}>
  {locations.map((item) => {
    if (item.cars != 0 || item.yachts != 0 || item.villas != 0)
      return (
        <DestinationItem
          id={item.id}
          image={Image[item.id]}
          name={item.name}
          subtitle={item.description}
          description={DESTINATIONS_DESCRIPTION[item.id]}
        />
      );
    return null;
  })}
</div>;
};

this is an item in locations object这是 object 位置中的项目

cars: 0
description: "(Miami, Palm Beach, Broward)"
id: 3
name: "South Florida"
photo: "http://l.amazonaws.com/areaphotos/Audi_R8_a.jpg"
villas: 69
yachts: 53
__proto__: Object

this is the whole component code:这是整个组件代码:

const Destinations = locations => {
 const renderItems = (locations) => {
  <div className={styles.list}>
  {locations.map(
    (item) =>
      (item.cars != 0 || item.yachts != 0 || item.villas != 0) && (
        <DestinationItem
          id={item.id}
          key={item.id}
          image={Image[item.id]}
          name={item.name}
          subtitle={item.description}
          description={DESTINATIONS_DESCRIPTION[item.id]}
        />
      )
  )}
</div>;

console.log(locations, 'data');
return (
  <div className={styles.mainContainer}>
    <div className={styles.title}>
      <h1>Pick Your Destination</h1>
        </div>
        {renderItems(locations)}
      </div>
    );
  };
};

export default Destinations;

在此处输入图像描述

Ok, so I'm not fully sure, but I try not to use if statements in the DOM.好的,所以我不完全确定,但我尽量不在 DOM 中使用if语句。 Try this style of conditional instead:试试这种风格的条件:

    <div className={styles.list}>
  {locations.map((item) => 
        (item.cars != 0 || item.yachts != 0 || item.villas != 0) && (
            <DestinationItem
              id={item.id}
              key={item.id}
              image={Image[item.id]}
              name={item.name}
              subtitle={item.description}
              description={DESTINATIONS_DESCRIPTION[item.id]}
            />)
  })
</div>;

The && operator acts as your if statement. &&运算符充当您的 if 语句。 And this way, you don't even need to include {} s in your map function.这样,您甚至不需要在 map function 中包含{} Also, don't forget to add a unique key to any mapped elements!另外,不要忘记为任何映射元素添加唯一键!

The Destinations component takes locations as a prop, correct? Destinations组件将locations作为道具,对吗? In that case, you'll want to either destructure the props object, like this:在这种情况下,您需要解构道具 object,如下所示:

const Destinations = ({ locations }) => {

or this或这个

const Destinations = props => {
  const { locations } = props

or just use the props object and access the fields separately:或者只使用道具 object 并分别访问这些字段:

const Destinations = props => {
  // ...
  return (
    // ...
    {renderItems(props.locations)}
    // ...
  )
}

Additionally, the renderItems function has curly braces around it, so you need to use return to return a value from it.此外, renderItems function 周围有花括号,因此您需要使用return从它返回一个值。 It's currently returning undefined , as there is no return .它目前正在返回undefined ,因为没有return

const renderItems = (locations) => {
  return (
    <div className={styles.list}>
      {locations.map((item) => {
        if (item.cars !== 0 || item.yachts !== 0 || item.villas !== 0)
          return (
            <DestinationItem
              id={item.id}
              image={Image[item.id]}
              name={item.name}
              subtitle={item.description}
              description={DESTINATIONS_DESCRIPTION[item.id]}
            />
          );
        return null;
      })}
    </div>
  );
};

Or you can use the concise form of an arrow function, and replace the curly braces with parentheses:或者您可以使用箭头 function 的简洁形式,并将花括号替换为括号:

const renderItems = (locations) => (
  <div className={styles.list}>
    {locations.map((item) => {
      if (item.cars !== 0 || item.yachts !== 0 || item.villas !== 0)
        return (
          <DestinationItem
            id={item.id}
            image={Image[item.id]}
            name={item.name}
            subtitle={item.description}
            description={DESTINATIONS_DESCRIPTION[item.id]}
          />
        );
      return null;
    })}
  </div>
);

As a side-note, you'll almost always want to use strict equality operators ( === and !== ) instead of abstract equality operators ( == and != ).作为旁注,您几乎总是希望使用严格相等运算符( ===!== )而不是抽象相等运算符( ==!= )。 The latter one has its share of pitfalls , eg '1' == 1 is true, as unexpected things like [[]] == 0 !后者有一些陷阱,例如'1' == 1是真的,就像[[]] == 0之类的意想不到的事情

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

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