Search code examples
reactjsreact-nativemovilizer

Creating a Custom Button react native


I'm trying to create a custom button in react native that have some default styles, but I want that the button allows me to change it's default styles for new ones depending the situation.

the code is this:

import React from 'react';
import {StyleSheet, TouchableOpacity, Text} from 'react-native';

export default function custom_button({txt, onPress}){
    return(
        <TouchableOpacity style={styles.container} onPress={onPress} >
            <Text style={styles.text}>{txt}</Text>
        </TouchableOpacity>
    )
}
const styles = StyleSheet.create({
    container:{
        backgroundColor:'#3F51B5',
        padding:10,
        margin:20,
        width:'80%',
        borderRadius:15,
        alignItems:'center'
    },
    text:{
        fontWeight:'bold', 
        color:'white', 
        fontSize:16
    }
});

I need a hero please


Solution

  • Its a very simple implementation:

    import React from 'react';
    import {StyleSheet, TouchableOpacity, Text} from 'react-native';
    
    export default function custom_button({txt, onPress, txtStyle, buttonStyle}){
        return(
             <TouchableOpacity style={[styles.container1,buttonStyle && buttonStyle]} onPress={onPress} >
                <Text style={[styles.text1,txtStyle && txtStyle]}>{txt}</Text>
            </TouchableOpacity>
        )
    }
    const styles = StyleSheet.create({
        container:{
            backgroundColor:'#3F51B5',
            padding:10,
            margin:20,
            width:'80%',
            borderRadius:15,
            alignItems:'center'
        },
        text:{
            fontWeight:'bold', 
            color:'white', 
            fontSize:16
        }
    });
    

    Also created a snack for you. You can experiment and play-around here.