Navigate back to the homepage

how to push data into array react native?

Infinitbility
React Native
June 24th, 2022 · 1 min read
how to push data into array react native?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To append or push data into an array in react native, just use the push() method it will add your data into an array as the last element.

Like the following example, we can push data into an array using the push() method.

1let arr = ["infinitbility","aguidehub"];
2
3arr.push("sortoutcode");
4
5console.log(arr); // ['infinitbility', 'aguidehub', 'sortoutcode']

Today, I’m going to show you How do i push data into an array in react native, as above mentioned here, I’m going to use the push() method for adding new elements into the array.

Let’s start today’s tutorial how do you push data into an array in react native?

In this example, we will do

  1. Create a sample array state with sample data
  2. Add a new element into the array state using the push() method
  3. Print array state in react native app screen

Let’s write code…

1import React, { useState, useEffect } from 'react';
2import { Text, View, StyleSheet } from 'react-native';
3
4const App = () => {
5 const [arr, setArr] = useState([]);
6
7 useEffect(() => {
8 let newArr = ["infinitbility","aguidehub"];
9 newArr.push("sortoutcode");
10 setArr(newArr);
11 }, []);
12
13 return (
14 <View style={styles.container}>
15 <Text>{arr.toString()}</Text>
16 </View>
17 );
18}
19
20const styles = StyleSheet.create({
21 container: {
22 flex: 1,
23 justifyContent: 'center',
24 backgroundColor: '#ecf0f1',
25 padding: 8,
26 },
27 paragraph: {
28 margin: 24,
29 fontSize: 18,
30 fontWeight: 'bold',
31 textAlign: 'center',
32 },
33});
34
35export default App;

Note: In the above code we have used the toString() method because react native can’t show arr comma separated show we have converted to string then print on the screen

As mentioned above, we are taken the example of an array of strings, use the push() to append an element, and printed it on the screen.

Let’s check the output.

React Native, add element in array example
React Native, add element in array example

Try it yourself

I hope it’s help 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 get string length in react native?

how to get string length in react native?

An example of get text length in react native with step by step guide.

June 23rd, 2022 · 1 min read
how to replace string in react native?

how to replace string in react native?

An example of replace string in react native with step by step guide.

June 23rd, 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]