I am testing some code to try and understand the `race condition` regarding the use of `setState()`.

my code can be found [here](https://codesandbox.io/embed/unique-id-generator-xeq2m?fontsize=14&hidenavigation=1&theme=dark)

my code below:

```
import React from "react";

export default class App extends React.Component {
  state = {
    id: "",
    ids: [{ id: 7 }, { id: 14 }]
  };

  // here is where I create the id numbers
  uniqueIdCreatorHandler = incrementAmount => {
    let ids = [...this.state.ids];
    let highestId = 0;
    if (ids.length > 0) {
      highestId = ids
        .map(value => {
          return value.id;
        })
        .reduce((a, b) => {
          return Math.max(a, b);
        });
    }
    let newId = highestId + incrementAmount;
    ids.push({ id: newId });
    this.setState({ ids: ids });
  };

  idDeleterHanlder = currentIndex => {
    let ids = this.state.ids;
    ids.splice(currentIndex, 1);
    this.setState({ ids: ids });
  };
  //below is when I test performing the function twice, in order to figure if the result would be a race condition
  double = (firstIncrementAmount, secondIncrementAmount) => {
    this.uniqueIdCreatorHandler(firstIncrementAmount);
    this.uniqueIdCreatorHandler(secondIncrementAmount);
  };

  render() {
    let ids = this.state.ids.map((id, index) => {
      return (
        <p onClick={() => this.idDeleterHanlder(index)} key={id.id}>
          id:{id.id}
        </p>
      );
    });

    return (
      <div className="App">
        <button onClick={() => this.uniqueIdCreatorHandler(1)}>
          Push new id
        </button>
        <button onClick={() => this.double(1, 2)}>Add some Ids</button>
        <p>all ids below:</p>
        {ids}
      </div>
    );
  }
}

```

when invoking the `double` function on the second `button` only the `secondIncrementAmount` works. You can test it by changing the argument values on the call made on the `onClick` method.

I think that I should somehow use `prevState` on `this.setState` in order to fix this.

How could I avoid this issue here? [This matter started at CodeReview](https://codereview.stackexchange.com/questions/237536/safe-way-of-creating-unique-ids) but I did not realize how could I fix this.

There is also a recommendation to spread the mapped ids into `Math.max` and I could not figure out how and Why to do it. Isn't the creation of the new `array` by mapping the spreaded key values safe enough?