Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

229
Vistas
How to conditionally overwrite a style with a different style in React Native?

I have a PrimaryButton element that has 3 variants - primary, secondary and tertiary. As you can see in the style of the Pressable component, I set the default style based on the variant like this styles[variant] . Now I also want to make the backgroundColor of that Pressable component to turn red while it is being pressed if the button variant is tertiary. I already have access to isPressed boolean that tells me if the Pressable is pressed, however, I couldn't figure out how to change the backgroundColor to red only if the variant is tertiary.

const PrimaryButton = ({ title, variant = 'primary', wide = false, style, ...rest }) => {
    const width = wide ? '100%' : undefined;
    const textColor = variant === 'primary' ? colors.white : colors.primary600;

    return (
        <Pressable
            style={({ pressed: isPressed }) => [
                styles.button,
                styles[variant],
                {
                    width,
                    elevation: isPressed ? 5 : 0,
                },
                style,
            ]}
            {...rest}
        >
        </Pressable>
    );
};

const styles = StyleSheet.create({
    button: {
        paddingVertical: 12,
        paddingHorizontal: 24,
        borderRadius: 100,
        borderWidth: 1.5,
        justifyContent: 'center',
        alignItems: 'center',
        alignSelf: 'center',
    },
    primary: {
        backgroundColor: colors.primary600,
        borderColor: colors.primary600,
    },
    secondary: {
        backgroundColor: colors.white,
        borderColor: colors.primary600,
    },
    tertiary: {
        backgroundColor: 'transparent',
        borderColor: 'transparent',
    },
    text: {
        textAlign: 'center',
    },
});
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

See if the following helps you. If not please do tell me what went wrong.

style = {({ pressed: isPressed }) => [
    styles.button,
    styles[variant],
    {
        width,
        elevation: isPressed ? 5 : 0,
        ...(variant === 'tertiary') ? { backgroundColor: 'red' } : {}
    },
    style,
]}
about 4 years ago · Juan Pablo Isaza Denunciar

0

check this package, very useful for this stuff. Styles directly on field level isn't recommended anymore. https://www.npmjs.com/package/isomorphic-style-loader Good luck

about 4 years ago · Juan Pablo Isaza Denunciar

0

In order to overwrite a style of a component, in this case, to change the backgroundColor to red only if the variant is tertiary, you can use the ternary operator.

It may be useful to access the defined styles to retrieve the background colors of the other buttons. To do that, you can use StyleSheet.flatten so that you do not override the previous color style applied.

style = {
    ({
        pressed: isPressed
    }) => [
        styles.button,
        styles[variant],
        {
            width,
            elevation: isPressed ? 5 : 0,
        },
        {
            backgroundColor: isPressed && variant === 'tertiary' ?
                'red' :
                StyleSheet.flatten(styles[variant]).backgroundColor
        },
        style,
    ]
}

As seen in this example.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda