Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React: duplicated item in list

Tags:

reactjs

I'm an absolute beginner in React. I created a simple app to show an integer list on a page. There is an 'add item' button and the app happens a new integer to the list when user presses the button. When I press the button, the new integer is added twice to the list. Can anyone help?

import React from 'react';

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      items: [],
      idx: 0
    }

    this.addItem = this.addItem.bind(this);
  }

  render() {
    return <div><h1>Learning app</h1>
    <ul>
      {this.state.items.map(item => (
        <li key={item}>{item}</li>
      ))}
    </ul>
    <button onClick={this.addItem}>Add Item</button>
    </div>;
  }

  addItem() {
    this.setState((state, props) => {
      state.items.push(state.idx)

      return {
        items: state.items,
        idx: state.idx + 1
      }
    })
  }
}

export default App;

When I put this inside addItem

    this.state.items.push(this.state.idx);
    this.setState({
      idx: this.state.idx + 1
    })

it works without any issue.

I deliberately use setState with arrow function for experiment purpose. Did I do something wrong?

like image 433
user3691191 Avatar asked Apr 08 '26 04:04

user3691191


1 Answers

You can try this, it will add a single item in the list

addItem = () => {
    this.setState({
      items: [...this.state.items, this.state.idx],
      idx: this.state.idx + 1,
    });
  };
like image 165
Mamunur Rashid Avatar answered Apr 09 '26 19:04

Mamunur Rashid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!