简体   繁体   English

将组件推送到数组并传递道具的方法

[英]method of pushing components to an array and also passing props

What is this method of pushing components to an array?这种将组件推送到数组的方法是什么?

days.push(

        <Day day={day}
          selected={selected}
          select={select}/>
      );

I read this code but couldn't understand few things in it.我阅读了这段代码,但无法理解其中的一些内容。

class Week extends React.Component {
  render() {
    let days = [];
    let {date,} = this.props;
    const { month,selected,select, } = this.props;
    console.log(`Inside weeks ${month.toString()}`);
    console.log(`Selected weeks ${selected.toString()}`);


    for (var i = 0; i < 7; i++) {

      let day = {
          name: date.format("dd").substring(0, 1),
          number: date.date(),
          isCurrentMonth: date.month() === month.month(),
          isToday: date.isSame(new Date(), "day"),
          date: date
      };



      days.push(

        <Day day={day}
          selected={selected}
          select={select}/>
      );
      console.log(`days inside return is ${days.selected}`);
      date = date.clone();
      date.add(1, "day");
    }


    return (

      <div className="row week" key={days[0]}>
        {days}
      </div>
    );
  }

}

I am quite puzzled at this step in code where apart from passing properties to another class ,it has also been pushed to an array and later same has been returned我对代码中的这一步感到非常困惑,除了将属性传递给另一个类之外,它还被推送到一个数组,后来同样被返回

<div className="row week" key={days[0]}>
        {days}
      </div>

This is a way to dynamically render element inside a loop.这是一种在循环内动态渲染元素的方法。 you render them and push them inside an array and then render the array (as the array is built of react elements, it will just render).你渲染它们并将它们推入一个数组中,然后渲染该数组(因为该数组是由 react 元素构建的,它只会渲染)。 Have a look here: Loop inside React JSX看看这里: 在 React JSX 中循环

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

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