# Help with react states

**URL:** <https://forum.freecodecamp.org/t/help-with-react-states/379862>\
**Category:** General\
**Created:** [April 27, 2020, 5:17pm UTC](https://forum.freecodecamp.org/t/help-with-react-states/379862 "2020-04-27T17:17:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SurajMishra](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SurajMishra](https://forum.freecodecamp.org/u/SurajMishra)\
**Post date:** [April 27, 2020, 5:17pm UTC](https://forum.freecodecamp.org/t/help-with-react-states/379862/1 "2020-04-27T17:17:51Z")

</div>

Here’s my code so far -

```auto
const [isEmpty, setIsEmpty] = useState(true);
let arr = [];

const addInterviewers = (value, isAdded) => {
   
   if (isAdded === 'ADD') {
     arr.push(value);
   } else {
     const index = arr.indexOf(value);
     arr.splice(index, 1)
   }
   
   enableButton();

   console.log(isEmpty);
   console.log(arr);
 }

 const enableButton = () => {
  if (arr.length === 0) {
    setIsEmpty(true);
  } else {
    setIsEmpty(false);
}  
}
   

<button
  className={
	isEmpty
	    ? "Interviewers-button"
	    : "Interviewers-button-enabled"
	}
> 
Next&emsp;&ensp; <FontAwesomeIcon icon="chevron-right"/>
</button>

```

The problem is, enableButton function is interfering with array ‘arr’ values. Because of enableButton, arr removes the first element and starts with second element.

Sorry for bad explanation. I hope it makes sense.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [April 27, 2020, 5:47pm UTC](https://forum.freecodecamp.org/t/help-with-react-states/379862/2 "2020-04-27T17:47:18Z")

</div>

I would need to see more of the code. Do you have a repo?

---

<div class="post-metadata">

**Author:** ![SurajMishra](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SurajMishra](https://forum.freecodecamp.org/u/SurajMishra)\
**Post date:** [April 27, 2020, 6:37pm UTC](https://forum.freecodecamp.org/t/help-with-react-states/379862/3 "2020-04-27T18:37:59Z")

</div>

Yes thanks! Here - [https://gitlab.com/Suraj-Mishra/feedback-app](https://gitlab.com/Suraj-Mishra/feedback-app)

---

<div class="post-metadata">

**Author:** ![DanCouper](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dancouper/32/158608_2.png) [@DanCouper](https://forum.freecodecamp.org/u/DanCouper)\
**Post date:** [April 27, 2020, 8:11pm UTC](https://forum.freecodecamp.org/t/help-with-react-states/379862/4 "2020-04-27T20:11:53Z")

</div>

Why is that array (`arr`) not being held in useState? Every time that component rerenders, it’s an empty array again, yet it seems like you’re trying to use it statefully: the handler that makes use of it is passed onto another component.

---

<div class="post-metadata">

**Author:** ![SurajMishra](https://avatars.discourse-cdn.com/v4/letter/s/848f3c/32.png) [@SurajMishra](https://forum.freecodecamp.org/u/SurajMishra)\
**Post date:** [April 28, 2020, 9:28am UTC](https://forum.freecodecamp.org/t/help-with-react-states/379862/5 "2020-04-28T09:28:43Z")

</div>

I solved the problem. Here’s the new code -

```auto
const [isEmpty, setIsEmpty] = useState(true);
  const [arr, setArr] = useState([]);

 const addInterviewers = (value, isAdded) => {
   
   if (isAdded === 'ADD') {
     let newList = arr;
     newList.push(value);
     setArr(newList);
   } else {
     let newList = arr;
     const index = newList.indexOf(value);
     newList.splice(index, 1);
     setArr(newList);
   }
   enableButton();
   console.log(isEmpty);
   console.log(arr);

   LocalForage.setItem('Name', arr);
 }

 const enableButton = () => {
  if (arr.length === 0) {
    setIsEmpty(true);
  } else {
    setIsEmpty(false);
}  
}
  

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:56pm UTC](https://forum.freecodecamp.org/t/help-with-react-states/379862/6 "2021-06-01T18:56:18Z")

</div>

Closed on June 1, 2021, 6:56pm UTC


