Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!