2

I have an issue I can't seem to crack even after checking out a few other posts on here and trying a few things out. I am playing around with React and making a quick todo list. Easy enough as I can add new names and display them on the page as intended. I want to be able to delete items that I choose and for that I was looking around and saw others doing something like this to delete items:

deleteName(id, e) {
    const { names } = this.state;

    this.setState({
        names: names.filter(name => name.id !== id)
    });
}

That made sense to me but I wasn't adding any id's to my <li> items so I thought I could just do:

this.setState({
   names: names.filter(name => name !== name)
});

But this will just delete the whole list. What am I doing wrong? Should I restructure how I add names to the array to have an id and check that? I'll post the full component code below. Any help I can get is always appreciated. Thanks guys.

class ContactListPage extends React.Component {
    constructor(props, context) {
      super(props, context);

      this.state = {
        names: []
      };

      this.addName = this.addName.bind(this);
      this.deleteName = this.deleteName.bind(this);
    }

  addName(name) {
    const { names } = this.state;

    if (name === '') {
        console.log('add a name first!')
    } else {
        this.setState({
          names: names.concat(name)
        });
    }
  }

  deleteName(id, e) {
    const { names } = this.state;

    this.setState({
        names: names.filter(name => name !== name)
    });
  }

  render() {
    const { names } = this.state;

    const named = names.map((name, i) => (
        <Card key={i} className='todo-list'>
            <CardText>
              <li>{name}    
                <FloatingActionButton className='fab-delete' mini onClick={this.deleteName}>
                           <i className="material-icons fab-icon-delete" style={{color: 'white'}}>-</i>
                        </FloatingActionButton>
              </li>
            </CardText>
        </Card>
    ));

    return (
        <div className='contact-list'>
          <div className="field-line">
            <NewName addName={this.addName} />
            <ul className='new-name'>
              {named}
            </ul>
          </div>
        </div>
    );
  }
}
justDan
  • 2,302
  • 5
  • 20
  • 29
  • 1
    What does this have to do with "Objects are not valid as a React child"? – Oliver Charlesworth May 23 '17 at 23:04
  • Oh wow I apologize @OliverCharlesworth. I was getting that error when I tried to concat an array of objects to my names array. Ex: `names: names.concat([{ name: name }]). – justDan May 23 '17 at 23:09
  • Can you confirm the data type of `names`. Is it just an array of strings? – fubar May 23 '17 at 23:11
  • Maybe your question is similar to one I asked before https://stackoverflow.com/questions/41436251/how-to-filter-through-array-of-components-elements-in-reactjs – A. L May 23 '17 at 23:11
  • @fubar It is indeed just an array of strings. When I tried to add objects to the names array, that's when I get the error `Objects are not valid as a React child`. A. Lau thank you for that link! That looks like it could help me out here. – justDan May 23 '17 at 23:13
  • When adding objects to the array, did you change your `
  • ` code to output a property of the object, rather than the entire object?
  • – fubar May 23 '17 at 23:15