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.

setArr([]);

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.

import React, { useState, useEffect } from "react";
import { Text, View } from 'react-native';

export default function App() {
  const [arr, setArr] = useState(["Infinitbility","aguidehub","sortoutcode"]);

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

  return (
    <View>
      <Text>{JSON.stringify(arr)}</Text>
    </View>
  );
}

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 đź‘Ť.

React Native, set empty array example
React Native, set empty array example

Try it yourself