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

从对象数组中删除项

  •  0
  • user3378165  · 技术社区  · 7 年前

    我有一个页面显示contatcs信息:

    contactsInfo.map((info, index) => (
                    <SearchResultPanel
                      info={info}
                      isAdmin={isAdmin}
                      key={index}
                      handleChange={this.makeHandleChange('searchResultPanel')}
                    />
                  )),
    

    contactsInfo 是一个数组 contactInfo 物体。

    SearchResultPanel

    <button onClick={() => this.handleDelete(info.id)}>
    

    handleDelete 功能:

    handleDelete = async contactID => {
        await deleteContact(contactID);
        this.setState(contactInfo:
            this.state.contactInfo.id === contactID? {} : this.state.contactInfo,
        }));
      };
    

    如果我按从高到低的顺序删除联系人 contacts[2] , contacts[1] contacts[0] 如果我按ex的升序删除联系人,一切正常 联系人[0] , 联系人[2] 第一 contact 被删除,但在 console.log 第二个触点的状态也正在清除 然而,第二个联系人仍然显示在页面上,当我点击它的删除按钮时,什么也没有发生。。。

    我到底做错了什么 导致这种行为的方法?

    componentDidUpdate

      componentDidUpdate(_, prevState) {
        const { handleChange } = this.props;
        if (this.state !== prevState) {
          handleChange(prevState.contactInfo.id);
        }
      }
    

    父母呢 handleChange 功能:

      makeHandleChange = panelName => contactID => {
        this.setState(prevState => ({
          contactsInfo: prevState.contactsInfo.filter(i => i.id !== contactID ),
        }));
      };
    

    整个父组件:

    import React from 'react';
    
    import SearchResultPanel from '../home/SearchResultPanel';
    
    const contactInformation = [
      {
        id: 123,
        routingNumber: 2634545,
        name: 'test',
        date: '01/23/2019',
      },
      {
        id: 1234,
        routingNumber: 2634546,
        name: 'test2',
        date: '01/23/2012',
      {
        id: 1235,
        routingNumber: 2634547,
        name: 'test3',
        date: '01/23/2012',
     },
    ];
    
    
    const getContacts = async () => {
      …
    };
    
    
    
    export default class Home extends React.Component {
      state = {
        contactsInfo: [],
        isAdmin: true,
      };
    
      handleSubmit = async () => {
        const contactsInfo = await getContacts();
        this.setState({contactsInfo });
      };
    
      makeHandleChange = panelName => contactID => {
        this.setState(prevState => ({
          contactsInfo: prevState. contactsInfo.filter(i => i.id !== contactID),
        }));
      };
    
      render() {
        const { contactsInfo, isAdmin } = this.state;
        return (
          <div>
            <div>
              <TextInput
                handleSubmit={this.handleSubmit}
              />
    
              {{
                  contactsInfo.map((info, index) => (
                    <SearchResultPanel
                      info={info}
                      isAdmin={isAdmin}
                      key={index}
                      handleChange={this.makeHandleChange('searchResultPanel')}
                    />
                  )),
            </div>
          </div>
        );
      }
    }
    

    整个子组件:

    import { Component } from 'react';
    import InfoItem from '../ui/InfoItem;
    
    
    const deleteContact = async contactID => {
    };
    
    export default class SearchResultPanel extends Component {
      state = {
        contactInfo: {},
      };
    
      componentDidMount() {
        const { info, handleChange } = this.props;
        this.setState({contactInfo: info });
        handleChange(this.state);
      }
    
      componentDidUpdate(_, prevState) {
        const { handleChange } = this.props;
        if (this.state !== prevState) {
          handleChange(prevState.contactInfo.id);
        }
      }
    
      handleDelete = async contactID => {
        await deleteContact(contactID);
        this.setState(prevState => ({
          contactInfo:
            prevState.contactInfo.id === contactID? {} : prevState.contactInfo,
        }));
      };
    
    
      render() {
        const { info, isAdmin } = this.props;
        const {
          routingNumber,
          name,
          date,
        } = info;
    
        return (
          <div>
            <div>
              <div>
                <div>
                  <InfoItem header="ROUTING NUMBER" detail={routingNumber} />
                  <InfoItem header="NAME" detail={name} />
             <InfoItem header="DATE" detail={date} />
                </div>
              </div>
                    <button
                      onClick={() => this.handleDelete(info.id)}
                    >
                      <DeleteIcon />
                    </button>
                  )}
                </div>
              </div>
            </div>
          </div>
        );
      }
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   r g    7 年前

    键有助于识别哪些项已更改、已添加或已删除 这些元素具有稳定的特性。

    在代码中,使用数组的索引作为键,这是不推荐的。

    如果项目的顺序可能会改变,我们不建议对键使用索引 改变这可能会对性能产生负面影响,并可能导致问题 具有组件状态。

    你应该使用你的 info.id 从…起 contact ,因为这样你就可以声明稳定的身份

    contactsInfo.map(info => (
                    <SearchResultPanel
                      info={info}
                      isAdmin={isAdmin}
                      key={info.id}
                      handleChange={this.makeHandleChange('searchResultPanel')}
                    />
                  )),
    

    资料来源: https://reactjs.org/docs/lists-and-keys.html

        2
  •  0
  •   Hitesh Chaudhari    7 年前

    您需要更新 handleDelete setState 作用

    handleDelete = async contactID => {
      await deleteContact(contactID);
      this.setState({
        contactInfo: this.state.contactInfo.id === contactID? {} : this.state.contactInfo,
      });
    };