Navigate back to the homepage

how to empty an array in react js?

Infinitbility
React
August 2nd, 2022 · 1 min read
how to empty an array in react js?

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.

1setArr([]);

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.

1import React, { useState, useEffect } from "react";
2export default function App() {
3 const [arr, setArr] = useState(["Infinitbility","aguidehub","sortoutcode"]);
4
5 useEffect(() => {
6 setArr([])
7 }, []);
8
9 return (
10 <div className="App">
11 <p>{JSON.stringify(arr)}</p>
12 </div>
13 );
14}

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 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to empty an array in react native?

how to empty an array in react native?

An example of an empty array in react native with the step-by-step guide.

August 2nd, 2022 · 1 min read
how to empty an array in typescript?

how to empty an array in typescript?

An example of an empty array in typescript with the step-by-step guide.

August 2nd, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]