简体   繁体   English

我无法通过反应创建新卡

[英]I can't create a new card with react

Problem: I want to write a program that adds a new card when the button is pressed. 问题:我想编写一个在按下按钮时添加新卡的程序。 I can connect and change states, but no new cards. 我可以连接和更改状态,但是没有新卡。 (There is no problem that the value in the state is constant. The important thing is the formation of the new card). (状态值恒定是没有问题的。重要的是形成新卡)。

There are two different components above. 上面有两个不同的组件。 When the button is pressed (same state), I want a new card to be created. 当按下按钮(相同状态)时,我想要创建一个新卡。 But I couldn't write the code. 但是我无法编写代码。


card.jsx card.jsx

import React from 'react'
import CardStyle from '../cardStyle/cardStyle';

class Card extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      isLoading: true,
      imageUrl: null,
      fullName: null,
      job: null
    };
  }

  clickEvent = () => {
    this.setState({
      fullName: 'Furkan',
      job: 'Software engineer'
    });
  }

  render() {
    let {isLoading, imageUrl, fullName, job} = this.state;
    return (
      <div>
        <CardStyle 
              isLoading={isLoading}
              imageUrl = {imageUrl}
              fullName = {fullName}
              job = {job}
              clicked = {this.clickEvent}
              />
        <button onClick={this.clickEvent}>ADD PERSON</button>
      </div>
    )
  }
}

export default Card;

cardStyle.jsx cardStyle.jsx

import React, { Component } from 'react'
import classes from "./cardStyle.css";
import Wrap from "../../Wrap/Wrap";

class CardStyle extends Component {
  state = {
    image: null,
    fullName: null,
    job: null
  };

  createCard = () => {
    return(
      <div className={classes.Card}>      
        <div className={classes.Image}>{this.props.imageUrl}</div>
        <div className={classes.Title}>{this.props.fullName}</div>
        <div className={classes.Job}>{this.props.job}</div>
      </div>
    )
  };

  render() {
    return (
      <Wrap>
        {this.createCard()}
      </Wrap>
    ) 
  }
}

export default CardStyle;

If you want to create a new card every time you click your button, you should create cards array which maps every new card to a component. 如果您想在每次单击按钮时创建一个新卡,则应该创建将所有新卡映射到组件的卡数组。 This way you will get a new card with every click instead of modifying the old card. 这样,您每次点击都会获得一张新卡,而不用修改旧卡。

import React from 'react'
import CardStyle from '../cardStyle/cardStyle';

class Card extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      cards: [
        {
          isLoading: true,
          imageUrl: null,
          fullName: null,
          job: null
        }
      ]
    };
  }

  clickEvent = () => {
    const cards = [
      ...this.state.cards,
      {
        fullName: 'Furkan',
        job: 'Software engineer'
      }
    ]; // This will create a new array from the old one with a new additional value
    this.setState({ cards });
  }

  render() {
    const { cards } = this.state;
    return (
      <div>
        {cards.map((card, index) => (
            <CardStyle 
              key={index}
              isLoading={card.isLoading}
              imageUrl = {card.imageUrl}
              fullName = {card.fullName}
              job = {card.job}
              />
        )}

        <button onClick={this.clickEvent}>ADD PERSON</button>
      </div>
    )
  }
}

export default Card;

When you're calling clickEvent method, you do not adding another value to the state , but you're overwrite existing: imgUrl and fullName . 调用clickEvent方法时,不会向state添加另一个值,但是会覆盖现有的值: imgUrlfullName

You have to create array of cards, and display them by map method inside return . 您必须创建纸牌数组,并通过return内部的map方法显示它们。

If first card is blank for now, you can start from empty array in state cards: [] , and display CardStyle component only if this.state.cards.length > 0 . 如果第一张卡CardStyle为空,则可以从状态cards: []空数组开始,并且仅在this.state.cards.length > 0显示CardStyle组件。

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

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