How to Create Random String in React Native?

04-Apr-2023

.

Admin

How to Create Random String in React Native?

Hi Guys,

This tutorial shows you react native random string example. if you have question about random string generator using react native then I will give simple example with solution. this example will help you how to create random string in react native. step by step explain how to implement random string in react native. you will do the following things for how to generator random string in react native.

Let's start following example:

Step 1: Download Project


In the first step run the following command to create a project.

expo init ExampleApp

Step 2: App.js

In this step, You will open the App.js file and put the code.

import React from 'react';

import { Button, StyleSheet, Text, View } from 'react-native';

const App = () => {

const [string, setString] = React.useState('String');

const generateRandomString = (lenth) => {

const char = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';

const random = Array.from(

{length: lenth},

() => char[Math.floor(Math.random() * char.length)]

);

const randomString = random.join("");

return setString(randomString);

}

return (

<View style={styles.container}>

<Text>{string}</Text>

<View style={styles.buttonContainer}>

<Button

title='generate random string'

onPress={() => generateRandomString(10)}

/>

</View>

</View>

);

}

const styles = StyleSheet.create({

container: {

flex:1,

justifyContent:'center',

alignItems:'center',

},

buttonContainer: {

marginTop:10,

},

});

export default App;

Run Project

In the last step run your project using the below command.

expo start

You can QR code scan in Expo Go Application on mobile.

Output :

It will help you...

#React Native