KamitoHiadaka
KamitoHiadaka

Reputation: 3

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

Upvotes: 0

Views: 869

Answers (3)

Gavin D&#39;mello
Gavin D&#39;mello

Reputation: 433

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.

Upvotes: 0

starkcontrarian416
starkcontrarian416

Reputation: 91

You should look into the "Button" from react native. make sure to import { Button } from 'react-native';

This is an example:

<Button
  onPress={onPressLearnMore}
  title="Learn More"
  color="#841584"
  accessibilityLabel="Learn more about this purple button"
/>

I also agree with Faris's answer, you can create global styles. Just change the style from container/text to something else

Check this out for more information https://reactnative.dev/docs/button

Upvotes: -1

Firas
Firas

Reputation: 13

You can make a globalstyle for the similar use , and when you need to modify the styles you can do this style={[styles.container,{add what you want to modify here}]} I hope that this will help you to solve your problem

Upvotes: 0

Related Questions