Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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>
    )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading