Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
How to have different text colour for 2 different imports of a custom component in React Native

I am having an issue changing the text colour on a few specific imports of a custom component.

I am able to change the border colour and background colour for the button itself depending on its use when I import it by changing the value of the props where it is used. However I cant work out how to do the same for the text. I imagine its something trivial but any help would be appreciated.

The code below is the code for the button.

import React from 'react';
import { StyleSheet, Text, TouchableOpacity } from 'react-native';
import colors from '../config/colors';


function AppButton({title, onPress, color = "primary", borderColor}) {
    return (
        <TouchableOpacity style={[
            styles.button, 
            {backgroundColor: colors[color], borderColor: colors[borderColor] }]} 
            onPress={onPress}
        >
            <Text style={[styles.text]}>{title}</Text>
        </TouchableOpacity>
    );
}

const styles = StyleSheet.create({
    button:{
        backgroundColor: colors.primary,
        borderRadius:15,
        padding: 15,
        width: "100%",
        marginVertical: 10,
        justifiedContent: 'center',
        alignItems: 'center',
        borderWidth: 6,
    },
    text:{
        color: colors.white,
        fontSize: 18,
        textTransform: 'uppercase',
        fontWeight: 'bold',
    }
})

export default AppButton;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Judging from your comment, there seems to be a misunderstanding on how this could work. I have created a little snack showcasing what you need.

Here is the code for completeness. I have created two buttons. One uses the default text color, the other one uses a different color which we have provided via props.

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

function AppButton({title, onPress, color = "blue", borderColor, textColor}) {
    return (
        <TouchableOpacity style={[
            styles.button, 
            {backgroundColor: color, borderColor: borderColor }]} 
            onPress={onPress}
        >
            <Text style={[styles.text, textColor && {color: textColor}]}>{title}</Text>
        </TouchableOpacity>
    );
}

export default function App() {
  return (
    <View style={{margin: 60}}>
      <AppButton textColor="red" title="TestButton" onPress={() => console.log("Hello World")} borderColor="yellow" />
      <AppButton title="TestButton" onPress={() => console.log("Hello World")} borderColor="yellow" />
    </View>
  );
}

const styles = StyleSheet.create({
    button:{
        backgroundColor: "red",
        borderRadius:15,
        padding: 15,
        width: "100%",
        marginVertical: 10,
        justifiedContent: 'center',
        alignItems: 'center',
        borderWidth: 6,
    },
    text:{
        color: "white",
        fontSize: 18,
        textTransform: 'uppercase',
        fontWeight: 'bold',
    }
})

The first one has a red text color, the second one is white.

Here is the result.

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda