Home > Back-end >  Pass useState function as props
Pass useState function as props

Time:08-12

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

CodePudding user response:

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>
    )
}
  • Related