Home React Native How to Generate Random Color in React Native?

How to Generate Random Color in React Native?

by Shahriar Sagor
Generate Random Color in React Native

Hello dev’s today Now in this post i will show you How to Generate Random Color in React Native? Here in this article i simple demonstrates of how to generate the random color in react native. Now in this tutorial i will give you a simple example of react native generate random color example. So this article i will give you a very simple example of how to implement random color in react native. So here i’m going to show you about the random color generator in react native. You just need to do some step to done this how to create random color in react native.

Step 1: Download Project

Now in the first step run the following command for create a project.

expo init ExampleApp
Step 2: CATEGORIES.js

Then First of all we need to create the Data folder inside our project. Now in this folder create CATEGORIES.js file.

So in this step, i will create the name of the object as CATEGORIEES. Export that object. we will also import this file to use this object.

after then, we will open the CATEGORIES.js file and then put the code.

Data/CATEGORIES.js

export const CATEGORIES = [
    {
        id:'1',
        category_name:'Mobile',
    },
    {
        id:'2',
        category_name:'HeadPhone',
    },
    {
        id:'3',
        category_name:'Computer',
    },
    {
        id:'4',
        category_name:'Laptop',
    },
    {
        id:'5',
        category_name:'AC',
    },
    {
        id:'6',
        category_name:'TV',
    },
    {
        id:'7',
        category_name:'Laptop',
    },
    {
        id:'8',
        category_name:'Refrigerator',
    },
    {
        id:'9',
        category_name:'Camera',
    },
    {
        id:'10',
        category_name:'Home Theater',
    },
];
Step 3: App.js

Now In this step, we will open the App.js file and then put the code.

import React from 'react';
import { StyleSheet, Text, View, StatusBar, Pressable, Platform, FlatList } from 'react-native';
import { CATEGORIES } from "./Data/CATEGORIES";

const randomNumber = () => {
    const generateRandomColor = Math.floor(Math.random() * 16777215)
        .toString(16)
        .padStart(6, '0');
    return `#${generateRandomColor}`;
}

const CategoryGridTil = ({ name, onPressd }) => {
    return (
        <View style={styles.greedItem}>
            <Pressable
                style={({ pressed }) => [styles.button, pressed ? styles.buttonPressed : null]}
                android_ripple={{ color: '#ccc' }}
                onPress={onPressd}
            >
                <View style={[styles.innerContainer, { backgroundColor: randomNumber() }]}>
                    <Text style={styles.title}>{name}</Text>
                </View>
            </Pressable>
        </View>
    );
}

const renderCategoryItem = (itemData) => {
    const pressHandler = () => {
        alert(itemData.item.id + '\n' + itemData.item.category_name);
    }

    return (
        <CategoryGridTil name={itemData.item.category_name} onPressd={pressHandler} />
    );
}

const App = () => {
    return (
        <View style={{ backgroundColor: '#965c0c' }}>
            <FlatList
                data={CATEGORIES}
                keyExtractor={(item) => item.id}
                renderItem={renderCategoryItem}
                numColumns={2}
            />
            <StatusBar />
        </View>
    );
}

const styles = StyleSheet.create({
    greedItem: {
        flex: 1,
        margin: 16,
        height: 150,
        borderRadius: 8,
        elevation: 4,
        backgroundColor: 'white',
        shadowColor: 'black',
        shadowOpacity: 0.25,
        shadowOffset: {
            width: 0,
            height: 2,
        },
        shadowRadius: 8,
        overflow: Platform.OS === 'android' ? 'hidden' : 'visible',
    },
    button: {
        flex: 1,
    },
    buttonPressed: {
        opacity: 0.5,
    },
    innerContainer: {
        flex: 1,
        padding: 16,
        borderRadius: 8,
        justifyContent: 'center',
        alignItems: 'center',
    },
    title: {
        fontWeight: 'bold',
        fontSize: 18,
        color: 'white',
    },
});

export default App;

Run Project

In the last step run our project by using the below command.

expo start

Read Also: How to use Dropdown in React Native?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like