代码之家  ›  专栏  ›  技术社区  ›  Gel

解释如何在REACT中卸下引导卡

  •  2
  • Gel  · 技术社区  · 7 年前

    import React, { Component } from "react";
    import FriendCard from "./components/FriendCard";
    import Wrapper from "./components/Wrapper";
    import Title from "./components/Title";
    import friends from "./friends.json";
    import "./App.css";
    
    class App extends Component {
      // Setting this.state.friends to the friends json array
      state = {
        friends
      };
    
      removeFriend = id => {
        // Filter this.state.friends for friends with an id not equal to the id being removed
        const friends = this.state.friends.filter(friend => friend.id !== id);
        // Set this.state.friends equal to the new friends array
        this.setState({ friends });
      };
    
      // Map over this.state.friends and render a FriendCard component for each friend object
      render() {
        return (
          <Wrapper>
            <Title>Friends List</Title>
            {this.state.friends.map(friend => (
              <FriendCard
                removeFriend={this.removeFriend}
                id={friend.id}
                key={friend.id}
                name={friend.name}
                image={friend.image}
                occupation={friend.occupation}
                location={friend.location}
              />
            ))}
          </Wrapper>
        );
      }
    }
    
    export default App;

    下面的代码片段允许我单击 card (引导css)并删除 卡片 . 它起作用了!但我的逻辑思维或方法是 on click, delete this card . 我在这段代码中看不到这一点,但它是有效的。我没有编程背景。

    我希望有人能给我解释一下,如果你认为的话,用外行的术语来理解它。也许可以让我再举几个例子。提前谢谢。

     removeFriend = id => {
    
        const friends = this.state.friends.filter(friend => friend.id !== 
    id);
         this.setState({ friends });
      };
    

    总体如下:

    以下是友元组件:

    import React from "react";
    import "./FriendCard.css";
    
    const FriendCard = props => (
      <div className="card">
        <div className="img-container">
          <img alt={props.name} src={props.image} />
        </div>
        <div className="content">
          <ul>
            <li>
              <strong>Name:</strong> {props.name}
            </li>
            <li>
              <strong>Occupation:</strong> {props.occupation}
            </li>
            <li>
              <strong>Location:</strong> {props.location}
            </li>
          </ul>
        </div>
        <span onClick={() => props.removeFriend(props.id)} className="remove">
          𝘅
        </span>
      </div>
    );
    
    export default FriendCard;
    3 回复  |  直到 7 年前
        1
  •  1
  •   the_cheff    7 年前

    那么你在这里发布的部分

    removeFriend = id => {
      const friends = this.state.friends.filter(friend => friend.id !== id);
      this.setState({ friends });
    };
    

    是你生命的最后一部分 逻辑思维 删除此卡

    它是在单击时调用的函数,但不处理实际的单击。 它将id作为输入,并将您的好友状态设置为与给定id匹配的所有元素。 这是通过filter函数完成的,您可以在此处阅读更多信息 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

    您的下一部分 逻辑思维 单击时 在FriendCard组件中处理

    在应用程序组件中,此行将处理程序函数作为属性注入FriendsCard组件

    removeFriends={this.removeFriends}
    

    这意味着无论何时单击你的removeFriends函数,它都会被FriendCard组件调用。

    实际的点击处理是在这一行的FriendCard组件中完成的

    <span onClick={() => props.removeFriend(props.id)} className="remove">
    
        2
  •  1
  •   Snowball    7 年前

    你的例子不完整。要真正理解它,您需要了解 <FriendCard> 组成部分然后你会注意到,逻辑可能实际上像你想象的那样工作。

    你看到了 FriendCard 组件在第2行导入。它在 render 从第22行开始的函数。

    这个 App 组件有一个状态对象,其中包含 friends 所有物对于状态对象中的每个朋友,我们渲染 友谊卡 组件,通过在第27行的状态上使用map方法进行迭代。 You can read more over the map method here .

    我们将几个属性传递给 友谊卡 组成部分我们传给 友谊卡 组件是方法 removeFriend 定义见第14行。

    删除好友 方法实际上负责从 state 对象如您所见,它收到 id 参数并筛选 状态 对象,用于具有不同 身份证件 而不是我们传入的id(本质上删除了我们传递给方法的id)。

    那个 删除好友 方法可能绑定到 click 中的处理程序 友谊卡 组件并在单击它时调用。

        3
  •  0
  •   jnicolacopoulos    7 年前

    以上两个答案都是正确的。我只想在使用此更新state属性时添加这一点。设置状态函数,此触发器会响应调用组件的渲染函数,该函数将根据新的状态属性(即已过滤的好友列表)重新渲染好友列表。