简体   繁体   English

如何在 React 中循环这个状态?

[英]How to loop this state in React?

I try to loop my state with a .map but it didn't work (I don't see my log in the console) ...我尝试使用 .map 循环我的状态,但它不起作用(我在控制台中没有看到我的日志)......

{this.state["cards"].map(card => console.log(card.title))}

Please tell me what's the error...请告诉我是什么错误...

I have my state here (which a see the log in my console) :我在这里有我的状态(在我的控制台中查看日志):

export default class Home extends Component {
  constructor(props) {
    super(props);
    this.state = {
      cards: []
    };
    fs.readdir(testFolder, (err, files) => {
      files.forEach(file => {
        this.state.cards.push({
          title: "Test1",
          pic:
            "https://seeklogo.com/images/C/confluence-logo-D9B07137C2-seeklogo.com.png",
          content: "Content",
          link: "#"
        });
      });
    });
    console.log(this.state); // I see this one
  }

Here is my console log :这是我的控制台日志:

Object {cards: Array[2]}
  cards: Array[2]
    0: Object
      title: "Test1"
      pic: "https://seeklogo.com/images/C/confluence-logo-D9B07137C2-seeklogo.com.png"
      content: "Content"
      link: "#"
    1: Object
      title: "Test1"
      pic: "https://seeklogo.com/images/C/confluence-logo-D9B07137C2-seeklogo.com.png"
      content: "Content"
      link: "#"

EDIT : Here is my render :编辑:这是我的渲染:

  render() {
    return (
      <div>
        <div className="container-fluid text-center">
          <h2 className="h2_title">Hi.</h2>
        </div>
        <div className="main">
          <ul className="cards">
            {this.state["cards"].map(cards => (
              <Card
                key={cards.title}
                link={cards.link}
                title={cards.title}
                pic={cards.pic}
              />
            ))}
          </ul>
        </div>
      </div>
    );
  }

He doesn't display my cards and if I replace it by console.log it didn't display it in my console他不显示我的卡片,如果我用 console.log 替换它,它也不会显示在我的控制台中

Thanks for helping me !谢谢你帮助我!

You need to make your operations inside the componentDidMount and update the state like this:您需要在 componentDidMount 中进行操作并更新状态,如下所示:

import React, {Component} from "react";

export default class Home extends Component {
    constructor(props) {
      super(props);
      this.state = {
        cards: [],
        loading: true
      };
    }

    componentDidMount() {

        let cards = [];

        fs.readdir(testFolder, (err, files) => {
            files.forEach(file => {
              cards.push({
                title: "Test1",
                pic:
                  "https://seeklogo.com/images/C/confluence-logo-D9B07137C2-seeklogo.com.png",
                content: "Content",
                link: "#"
              });
            });

            this.setState({
                cards,
                loading: false
            })
          });

    }


    render() {

        const {cards, loading} = this.state;

        if (loading) {
            return (
                <div>Getting Files, please wait...</div>
                )
        }

        if (cards.length === 0) {
            return (
            <div>No files found</div>
            )
        }

        return (
          <div>
            <div className="container-fluid text-center">
              <h2 className="h2_title">Hi.</h2>
            </div>
            <div className="main">
              <ul className="cards">
                {cards.map(card => (
                  <Card
                    key={card.title}
                    link={card.link}
                    title={card.title}
                    pic={card.pic}
                  />
                ))}
              </ul>
            </div>
          </div>
        );
      }
}

You shouldn't directly change the state like this this.state.cards.push你不应该像这样直接改变状态this.state.cards.push

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

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