Hi Friends 👋,
Welcome To Infinitbility! ❤️
To empty an array in react native, 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 native, 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 native?
React native empty an array example using the []
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";2import { Text, View } from 'react-native';34export default function App() {5 const [arr, setArr] = useState(["Infinitbility","aguidehub","sortoutcode"]);67 useEffect(() => {8 setArr([])9 }, []);1011 return (12 <View>13 <Text>{JSON.stringify(arr)}</Text>14 </View>15 );16}
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.