Navigate back to the homepage

how to convert array to string in react native?

Infinitbility
React Native
June 20th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To convert an array to a string in react native, we can use the toString() method to convert an array to a string but if want to convert an array of objects into a string you have to use the JSON.stringify() method else it will show something like "[object Object]".

Like the following example, we can convert array to string useing toString() and JSON.stringify() methods.

1["hello", "shani"].toString(); // 'hello,shani'
2JSON.stringify([{greeting: "hello"}]); // '[{"greeting":"hello"}]'

Today, I’m going to show you How do I convert the array to string react native, as above mentioned here, I’m going to use the toString() and JSON.stringify() methods to convert an array and array of objects to a string.

Let’s start today’s tutorial how do you convert array to string react native?

In this example, we will do

  1. Create a sample array of strings and array of an object variables.
  2. Use toString() and JSON.stringify() method
  3. Print the Output on the screen
1import React, { useState, useEffect } from 'react';
2import { Text, View, StyleSheet } from 'react-native';
3
4export default function App() {
5 const [arrOfStr, setArrOfStr] = useState([]);
6 const [arrOfObj, setArrOfObj] = useState([]);
7
8 useEffect(() => {
9 setArrOfStr(["hello", "shani"]);
10 setArrOfObj([{greeting: "hello"}]);
11 }, []);
12
13 return (
14 <View style={styles.container}>
15 <Text>{'Array to string'} - {arrOfStr.toString()}</Text>
16 <Text>{'Array of object to string'} - {JSON.stringify(arrOfObj)}</Text>
17 </View>
18 );
19}
20
21const styles = StyleSheet.create({
22 container: {
23 flex: 1,
24 justifyContent: 'center',
25 backgroundColor: '#ecf0f1',
26 padding: 8,
27 },
28 paragraph: {
29 margin: 24,
30 fontSize: 18,
31 fontWeight: 'bold',
32 textAlign: 'center',
33 },
34});

As above mentioned, we are taken the example of an array of variables, converted it into a string, and printed on the screen.

Let’s check the output.

React Native, convert array to string example
React Native, convert array to string 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 concat string in react native?

Example of concat string in react native with step by step guide.

June 19th, 2022 · 1 min read

how to convert json object to array in react native?

Example of to convert json object to array in react native with step by step guide.

June 19th, 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]