0

I am currently trying to create a Modal object using react-native-modal like this

const ModalExample = (props) => {
    return (
        <Modal isVisible={props.isVisible}>
               <Text>Hello!</Text>
               <Button title="Hide modal" onPress={this.setModalVisibility} />
        </Modal>
    )
}

The problem is that the onPress is supposed to change the state from true to false but doesn't have access to the state within the class. Here is the button that initializes the modal and how i'm calling the object:

<Button title="Show modal" onPress={this.setModalVisibility} />
<ModalExample isVisible={this.state.modalVisible}/>

and just incase here is my setModalVisibility that is within the class

setModalVisibility = () =>{
     this.setState({modalVisible: !this.state.modalVisible})
}

Thanks for any help

0

1 Answer 1

1

You need to pass the function as well,

<ModalExample isVisible={this.state.modalVisible} setModalVisibility={this.setModalVisibility}/>

Call that function in modal,

const ModalExample = (props) => {
    return (
        <Modal isVisible={props.isVisible}>
             <Text>Hello!</Text>
             <Button title="Hide modal" onPress={props.setModalVisibility} />
        </Modal>
    )
}

Note: You don't have access to this in functional component.

Sign up to request clarification or add additional context in comments.

1 Comment

If it helps, please mark it as the solution and close the question. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.