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

183
Vistas
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 Respuestas
Responde la pregunta

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 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