1
resposta

Reaproveitando o botão

Detalhes.tsx


import { Image, StyleSheet, View } from "react-native";

import Texto from "../../../componentes/Texto";
import { TextoCesta } from "../../../mocks/interfaces";
import Botao from "./Botao";

export default function Detalhes(textoCesta: TextoCesta) {

    return <>
        <Texto style={estilos.nome}>{textoCesta.detalhes.nome}</Texto>
        <View style={estilos.fazenda}>
            <Image source={textoCesta.detalhes.logoFazenda} style={estilos.imagemFazenda}></Image>
            <Texto style={estilos.nomeFazenda}>{textoCesta.detalhes.nomeFazenda}</Texto>

        </View>
        <Texto style={estilos.descricao}>
            {textoCesta.detalhes.descricao}
        </Texto>
        <Texto style={estilos.preco}>{textoCesta.detalhes.preco}</Texto>
        <Botao style={estilos.botao} onPress={() => { console.log("OLA!") }}> {textoCesta.detalhes.botao}</Botao>
    </>
}

const estilos = StyleSheet.create({
    nome: {
        color: "#464646",
        fontSize: 26,
        lineHeight: 42,
        fontWeight: "bold"
    },
    fazenda: {
        flexDirection: "row",
        paddingVertical: 12
    },
    imagemFazenda: {
        width: 32,
        height: 32
    },
    nomeFazenda: {
        fontSize: 16,
        lineHeight: 26,
        marginLeft: 12
    },
    descricao: {
        color: "#A3A3A3",
        fontSize: 16,
        lineHeight: 26
    },
    preco: {
        color: "#2A9F85",
        fontWeight: "bold",
        fontSize: 26,
        lineHeight: 42,
        marginTop: 8
    },
    botao: {
        marginTop: 16
    }
})

Botao.tsx


import React from "react";
import { GestureResponderEvent, StyleProp, StyleSheet, TouchableOpacity, ViewStyle } from "react-native";
import Texto from "../../../componentes/Texto";

type Props = {
    children: React.ReactNode;
    style?: StyleProp<ViewStyle>;
    onPress?: (event: GestureResponderEvent) => void;
}

export default function Botao({ children, style, onPress }: Props) {
    return <>
        <TouchableOpacity style={[style, estilos.botao]} onPress={onPress}>
            <Texto style={estilos.textoBotao}>{children}</Texto>
        </TouchableOpacity>
    </>
}

const estilos = StyleSheet.create({
    botao: {
        backgroundColor: "#2A9F85",
        paddingVertical: 16,
        borderRadius: 6
    },
    textoBotao: {
        textAlign: "center",
        color: "#FFFFFF",
        fontSize: 16,
        lineHeight: 26,
        fontWeight: "bold"
    }
})
1 resposta

Olá, Henrique! Tudo bem?

Uma forma de fazer isso é criar um arquivo separado para o componente de botão e importá-lo onde for necessário. No seu caso, o arquivo "Botao.tsx" já está fazendo isso.

No arquivo "Detalhes.tsx", você já está importando o componente de botão corretamente:

import Botao from "./Botao";

E está utilizando-o dentro do seu retorno de função:

<Botao style={estilos.botao} onPress={() => { console.log("OLA!") }}> {textoCesta.detalhes.botao}</Botao>

Dessa forma, você já está reaproveitando o botão em diferentes partes do seu aplicativo. Basta importar o componente "Botao" e utilizá-lo onde for necessário.

Espero ter ajudado e bons estudos!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software