Hi Friends 👋,

Welcome To Infinitbility! ❤️

To empty an array in react js, set [] empty array in a state it will remove the existing array of elements and store a new empty array in a state.


Today, I’m going to show you How do I empty an array in react js, as above mentioned, I’m going to use the above-mentioned set [] empty array syntax method to empty an array.

Let’s start today’s tutorial how do you empty an array in react js?

React js empty an array example using [] method

Here, we will create an array state with some data and then we will use [] to remove all elements from the array state and set an empty array.

import React, { useState, useEffect } from "react";
export default function App() {
  const [arr, setArr] = useState(["Infinitbility","aguidehub","sortoutcode"]);

  useEffect(() => {
  }, []);

  return (
    <div className="App">

In the above example, I have created an array state and emptied it in useEffect after that printed output in the screen,

I hope it helps you, All the best 👍.