axios 在调用第三方API后,如何为每个单独的Map项添加其他属性?

5cnsuln7  于 2022-11-05  发布在  iOS
关注(0)|答案(1)|浏览(174)

我想添加一个特性,增加某人获得类似FB的“喜欢”的数量。配置文件通过第三方API通过Axios GET请求传入。当用户单击“喜欢”按钮时,某人得到的赞的数量应该以1为增量。我以前在handleClicks中编写的代码()将每个人的喜欢值加1,而不是只加一个人的喜欢值。数据以一个块的形式传递到卡片[]中。
App.js

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
class App extends React.Component {

constructor(props) {
    super(props)
    this.state = {cards: [], numVotes: 0};
    this.handleClick = this.handleClick.bind(this);
  }
  handleClick() {
    console.log("This is working!");
    this.setState(numVotes: state.numVotes + 1})
  }

  componentDidMount() {
    axios.get('/')
    .then(res => {
      this.setState({cards: res.data})
      console.log(this.state);
    })
  }
  render() {
      return (
        <div className="main-container">
        <Header />
        <CardList
          cards={this.state.cards}
          handleClick={this.handleClick}
        />
        <hr className="ui divider"></hr>
        </div>
      );
  }
export default App;
const CardList = props => {
  const cards = props.cards.map(card => {
    return <Card image={card.image_url}
          name={card.name}
          title={card.title}
          blurb={card.bio}
          handleClick={props.handleClick}
          numVotes={props.numVotes}
    />
  })
  return <div className="ui divided items">
            {cards}
         </div>
}

Card.js

const Card = (props) => {
  return (
  <div className="card-component item">
    <div class="ui small rounded image">
      <img className="portrait"
           src = {props.image}
           onError={(e)=>{e.target.onerror = null; e.target.src='https://image.shutterstock.com/image-vector/no-image-available-vector-illustration-260nw-744886198.jpg'}}
      />
    </div>
    <div class="content">
      <a className="header">{props.name}</a>
      <div class="meta">
        <span className="title">{props.title}</span>
      </div>
      <p className="blurb">{props.blurb}</p>
      <p><span className="question"> Want to work with {props.name}?</span>
        <span className="like-button" onClick={props.handleClick}>
          <img className="icon" src={icon} />Yes!
        </span>
      </p>
      <p className="yes-amt">{props.numVotes} people have said Yes!</p>
    </div>
  </div>
  )
}
qhhrdooz

qhhrdooz1#

首先,您需要确定如何将每张卡标识为唯一的,以便更新正确的卡。如果您有一个id,这将是理想的,但我将假设name是唯一的,因为它是您的问题中的一个值。

// pass the unique identifier to handler
handleClick(name) {
  this.setState((prevState) => ({
    // map over the previous cards and return a new array
    cards: prevState.cards.map((card) => {
      // If the name matches the current card, change it
      if (card.name === name) {
        return {...card, numVotes: card.numVotes + 1};
      } else {
        // Otherwise just return the same card unchanged.
        return card;
      }
    })
  }))
}

然后在您的组件中使用它,如下所示:

// Use inline function so we can pass it a prop as a parameter
<span className="like-button" onClick={() => props.handleClick(props.name)}>

相关问题