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

184
Visualizações
Pass useState function as props

I'm trying to pass a useState setState function to a custom component. Every option that I've tried so far has failed.

This is where I call my custom component:

const [showDialog, setShowDialog] = useState(true);

<DisclaimerModal
     text='...'
     showDialog={showDialog}
     handleAccept={setShowDialog(false)}
     handleDecline={setShowDialog(false)}
/>

And this is my custom component:

interface DisclaimerModalProps extends ViewProps {
    text: string,
    showDialog: boolean,
    handleAccept: () => void,
    handleDecline: () => void
}

export function DisclaimerModal({ text, showDialog, handleAccept, handleDecline }: DisclaimerModalProps): JSX.Element {
    return (
        <Modal
            visible={showDialog}
            transparent
        >
            <View style={styles.centeredView}>
                <View style={styles.modalView}>
                        <Text style={styles.textDisclaimer}>{text}</Text>
                        <View style={styles.modalRow}>
                            <Text
                                onPress={() => { handleDecline }}
                            >
                                Cancel
                            </Text>
                            <Text
                                onPress={() => { handleAccept }}
                            >
                                Accept
                            </Text>
                        </View>
                    </View>
                </View>
        </Modal>
    )
}

How can I pass the useState function as a prop? As you can see, the last thing I tried was to pass the whole function, however, this doesn't seem to work

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are calling setState straight away and returning the result (which is probably undefined, the setter doesnt return anything) to the prop. What you actually want to do is to provide a function which calls the setter.

const [showDialog, setShowDialog] = useState(true);

<DisclaimerModal
     text='...'
     showDialog={showDialog}
     handleAccept={() => setShowDialog(false)}
     handleDecline={() => setShowDialog(false)}
/>

And then in the consuming component, call the function you just passed.

interface DisclaimerModalProps extends ViewProps {
    text: string,
    showDialog: boolean,
    handleAccept: () => void,
    handleDecline: () => void
}

export function DisclaimerModal({ text, showDialog, handleAccept, handleDecline }: DisclaimerModalProps): JSX.Element {
    return (
        <Modal
            visible={showDialog}
            transparent
        >
            <View style={styles.centeredView}>
                <View style={styles.modalView}>
                        <Text style={styles.textDisclaimer}>{text}</Text>
                        <View style={styles.modalRow}>
                            <Text
                                onPress={() => { handleDecline() }}
                            >
                                Cancel
                            </Text>
                            <Text
                                onPress={() => { handleAccept() }}
                            >
                                Accept
                            </Text>
                        </View>
                    </View>
                </View>
        </Modal>
    )
}
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