简体   繁体   English

如何通过 for 循环在 React class 组件中创建对象数组以映射到功能组件中?

[英]How do I create an array of objects in a React class component through a for loop to be mapped in a functional component?

I created the following class component with React:我使用 React 创建了以下 class 组件:

I want the daysArrayState object to hold an array of objects that is create through the addDay arrow function.我希望 daysArrayState object 保存通过 addDay 箭头 function 创建的对象数组。

 state = {
    daysArray: []
}

days = () => {
    let daysArrayCopy = [];
    for(let i=1; i >= 35; i++){
        let id = Math.random();
        let num = i;
        let day = {
            dayId: id,
            dayNum: num
        }
        daysArrayCopy = [...this.state.daysArrayState, day];
        this.setState({daysArray: daysArrayCopy});
    }
}


render(){

    this.days();

    return(
        <div className="grid calendar-template">
            <div className="grid">Left</div>
            <div>
                <div className="grid text-center month-title">
                    January
                </div>
                <div>
                    <DayBox days = {this.state.daysArray}></DayBox>
                </div>
            </div>
            <div className="grid">Right</div>
        </div>
    )
}

I have another functional component that is meant to map through the daysArrayState object:我有另一个功能组件,用于 map 通过 daysArrayState object:

const DayBox = ({days}) => {

const daysList = days.map(day => { 
    return(
        <div className="grid dayBox-template text-center" key={day.dayId}>
            {day.dayNum}
        </div>
    )
})

return(
    <div className="grid week-grid">
        {daysList}
    </div>
)    

When I try to do this I get an error as the parameter I appear to be passing is the addDay function and not the array of objects in daysArrayState.当我尝试执行此操作时出现错误,因为我传递的参数似乎是 addDay function 而不是 daysArrayState 中的对象数组。

Here is your main component:这是您的主要组件:

class App extends Component {
  state = {
    days: []
  };
  //call the function in this lifecycle method
  componentDidMount() {
    this.addDay();
  }
  //see the updated logic for your addDay
  addDay = () => {
    const daysArrayCopy = [];
    for (let i = 0; i <= 35; i++) {
      let id = Math.random();
      let num = i;
      let day = {
        dayId: id,
        dayNum: num
      };
      daysArrayCopy.push(day);
    }
    this.setState({ days: [...this.state.days, ...daysArrayCopy] });
  };

  render() {
    return (
      <div className="grid calendar-template">
        <div className="grid">Left</div>
        <div>
          <div className="grid text-center month-title">January</div>
          <div>
            <DayBox days={this.state.days} /> //pass the state here
          </div>
        </div>
        <div className="grid">Right</div>
      </div>
    );
  }
}

Here is your DayBox component:这是您的DayBox组件:

export default function DayBox({ days }) {
  return (
    <div className="grid week-grid">
      {days.map(day => (
        <div key={day.dayId}>{day.dayNum}</div>
      ))}
    </div>
  );
}

Here is a live demo: https://stackblitz.com/edit/react-zvzfwu?file=index.js这是一个现场演示: https://stackblitz.com/edit/react-zvzfwu?file=index.js

暂无
暂无

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

相关问题 如何在React组件中遍历数组内部对象的属性? - How to loop through properties of objects inside of an array within a React component? 我如何编写此类组件来响应功能组件。? - How can i write this class component to react functional component.? 反应:如何访问 class 组件中的功能组件的道具 - React: How can I acces to props of a functional component in a class component 我怎样才能把这个 React class 组件变成一个功能组件? - How can i turn this React class component into a functional component? 如何将我的 react js 类组件转换为功能组件? - How do I convert my react js class component to a functional component? 如何从 class 组件更新功能组件的 state 以便在反应中显示 map 中的项目 - How do I update state of a functional component from a class component in order to display items from a map in react 反应功能组件-当组件返回元素数组时,如何将引用传递回父级? - React functional component - how do I pass refs back to parent when component returns an array of elements? 如何循环遍历子数组并检查组件是否存在 React 测试库 - How do I loop through child array and check if component exists React Testing Library 如何通过 React Router Link 将值从功能组件传递到另一个功能组件? - How can I pass values from a functional component through a React Router Link to another functional component? 将类组件反应为功能组件 - React class component to functional component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM