Closing modal in child component React Native
I have two components in react native and I'm unable to close a modal from my child component.
ListTrips - Parent
ModalAddTrip - Child
ListTrips.js
import ModalAddTrip from './ModalAddTrip';
....
....
this.state = {
isModalAddTripVisible: false
}
....
handleDismissModalAddTrip = () => {
this.setState({ isModalAddTripVisible: false });
};
closeModal() {
this.refs.ModalAdd.handleDismissModalAddTrip();
}
....
<ModalAddTrip
ref="ModalAdd"
isVisible={this.state.isModalAddTripVisible}
onBackDropPress={this.handleDismissModalAddTrip}
closeModal={() => this.closeModal()}
onRequestClose={this.handleDismissModalAddTrip}
/>
ModalAddTrip.js
<Modal
isVisible={isVisible}
onBackdropPress={onBackDropPress}
closeModal={this.props.child}
>
<Button
style={{ fontSize: 18, color: 'white' }}
containerStyle={{
padding: 8,
marginLeft: 70,
}}
onPress={this.closeModal}
>
I'm unable to close the modal once I open it. I know its something to do with referencing/props but I've messed around with it for hours and I cannot get anywhere. I was trying something along the lines of this.props.closeModal;
along with switching the ref to the child component but it didn't work either. inside a function in ModalAddTrip but that wouldn't work either.
Any help is greatly appreciated.
Thanks
react-native modal-dialog ref react-props
add a comment |
I have two components in react native and I'm unable to close a modal from my child component.
ListTrips - Parent
ModalAddTrip - Child
ListTrips.js
import ModalAddTrip from './ModalAddTrip';
....
....
this.state = {
isModalAddTripVisible: false
}
....
handleDismissModalAddTrip = () => {
this.setState({ isModalAddTripVisible: false });
};
closeModal() {
this.refs.ModalAdd.handleDismissModalAddTrip();
}
....
<ModalAddTrip
ref="ModalAdd"
isVisible={this.state.isModalAddTripVisible}
onBackDropPress={this.handleDismissModalAddTrip}
closeModal={() => this.closeModal()}
onRequestClose={this.handleDismissModalAddTrip}
/>
ModalAddTrip.js
<Modal
isVisible={isVisible}
onBackdropPress={onBackDropPress}
closeModal={this.props.child}
>
<Button
style={{ fontSize: 18, color: 'white' }}
containerStyle={{
padding: 8,
marginLeft: 70,
}}
onPress={this.closeModal}
>
I'm unable to close the modal once I open it. I know its something to do with referencing/props but I've messed around with it for hours and I cannot get anywhere. I was trying something along the lines of this.props.closeModal;
along with switching the ref to the child component but it didn't work either. inside a function in ModalAddTrip but that wouldn't work either.
Any help is greatly appreciated.
Thanks
react-native modal-dialog ref react-props
add a comment |
I have two components in react native and I'm unable to close a modal from my child component.
ListTrips - Parent
ModalAddTrip - Child
ListTrips.js
import ModalAddTrip from './ModalAddTrip';
....
....
this.state = {
isModalAddTripVisible: false
}
....
handleDismissModalAddTrip = () => {
this.setState({ isModalAddTripVisible: false });
};
closeModal() {
this.refs.ModalAdd.handleDismissModalAddTrip();
}
....
<ModalAddTrip
ref="ModalAdd"
isVisible={this.state.isModalAddTripVisible}
onBackDropPress={this.handleDismissModalAddTrip}
closeModal={() => this.closeModal()}
onRequestClose={this.handleDismissModalAddTrip}
/>
ModalAddTrip.js
<Modal
isVisible={isVisible}
onBackdropPress={onBackDropPress}
closeModal={this.props.child}
>
<Button
style={{ fontSize: 18, color: 'white' }}
containerStyle={{
padding: 8,
marginLeft: 70,
}}
onPress={this.closeModal}
>
I'm unable to close the modal once I open it. I know its something to do with referencing/props but I've messed around with it for hours and I cannot get anywhere. I was trying something along the lines of this.props.closeModal;
along with switching the ref to the child component but it didn't work either. inside a function in ModalAddTrip but that wouldn't work either.
Any help is greatly appreciated.
Thanks
react-native modal-dialog ref react-props
I have two components in react native and I'm unable to close a modal from my child component.
ListTrips - Parent
ModalAddTrip - Child
ListTrips.js
import ModalAddTrip from './ModalAddTrip';
....
....
this.state = {
isModalAddTripVisible: false
}
....
handleDismissModalAddTrip = () => {
this.setState({ isModalAddTripVisible: false });
};
closeModal() {
this.refs.ModalAdd.handleDismissModalAddTrip();
}
....
<ModalAddTrip
ref="ModalAdd"
isVisible={this.state.isModalAddTripVisible}
onBackDropPress={this.handleDismissModalAddTrip}
closeModal={() => this.closeModal()}
onRequestClose={this.handleDismissModalAddTrip}
/>
ModalAddTrip.js
<Modal
isVisible={isVisible}
onBackdropPress={onBackDropPress}
closeModal={this.props.child}
>
<Button
style={{ fontSize: 18, color: 'white' }}
containerStyle={{
padding: 8,
marginLeft: 70,
}}
onPress={this.closeModal}
>
I'm unable to close the modal once I open it. I know its something to do with referencing/props but I've messed around with it for hours and I cannot get anywhere. I was trying something along the lines of this.props.closeModal;
along with switching the ref to the child component but it didn't work either. inside a function in ModalAddTrip but that wouldn't work either.
Any help is greatly appreciated.
Thanks
react-native modal-dialog ref react-props
react-native modal-dialog ref react-props
edited Nov 23 '18 at 21:33
Remul
1,1451213
1,1451213
asked Nov 23 '18 at 16:01
Oscar FenOscar Fen
177
177
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Here is a solution which I use to handle modal.
export default class MyModal extends React.Component<Props, State>{
constructor(props: Props){
super(props);
this.state = {
visible: false,
}
}
// Use this method to toggle the modal !
toggleModal = () => {
this.setState({visible: !this.state.visible});
}
render(){
return(
<Modal
isVisible={this.state.visible}
hideModalContentWhileAnimating
onBackdropPress={() => {
this.toggleModal();
}}
useNativeDriver
>
<View style={{backgroundColor: "white", padding: 5}}>
</View>
</Modal>
);
}
}
If I press behind it, the modal will close -> it can close itself.
Now to manage it from the parent component just get a ref from your modal :
<MyModal
ref={ref => {
this.myModal = ref;
}}
/>
And you can toggle it from the parent component :
toggleMyModal = () => {
if(this.myModal){
this.myModal.toggleModal();
}
}
Let me know if you got it working :)
It worked thanks!
– Oscar Fen
Nov 25 '18 at 15:51
No problem @OscarFen ! Please consider accepting the answer as solution ;)
– Hugo Laplace
Nov 25 '18 at 16:39
I don't have enough reputation :(
– Oscar Fen
Nov 25 '18 at 16:49
You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project
– Hugo Laplace
Nov 25 '18 at 16:53
add a comment |
ModalAddTrip.js
_toggleModal = () =>
this.setState({ isVisible: !this.state.isVisible });
....
render() {
....
<Modal
isVisible={isVisible}
onBackdropPress={onBackDropPress}
>
<Button
style={{ fontSize: 18, color: 'white' }}
containerStyle={{
padding: 8,
marginLeft: 70,
}}
onPress={this._toggleModal} >
</Modal>
didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?
– Oscar Fen
Nov 23 '18 at 17:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449752%2fclosing-modal-in-child-component-react-native%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a solution which I use to handle modal.
export default class MyModal extends React.Component<Props, State>{
constructor(props: Props){
super(props);
this.state = {
visible: false,
}
}
// Use this method to toggle the modal !
toggleModal = () => {
this.setState({visible: !this.state.visible});
}
render(){
return(
<Modal
isVisible={this.state.visible}
hideModalContentWhileAnimating
onBackdropPress={() => {
this.toggleModal();
}}
useNativeDriver
>
<View style={{backgroundColor: "white", padding: 5}}>
</View>
</Modal>
);
}
}
If I press behind it, the modal will close -> it can close itself.
Now to manage it from the parent component just get a ref from your modal :
<MyModal
ref={ref => {
this.myModal = ref;
}}
/>
And you can toggle it from the parent component :
toggleMyModal = () => {
if(this.myModal){
this.myModal.toggleModal();
}
}
Let me know if you got it working :)
It worked thanks!
– Oscar Fen
Nov 25 '18 at 15:51
No problem @OscarFen ! Please consider accepting the answer as solution ;)
– Hugo Laplace
Nov 25 '18 at 16:39
I don't have enough reputation :(
– Oscar Fen
Nov 25 '18 at 16:49
You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project
– Hugo Laplace
Nov 25 '18 at 16:53
add a comment |
Here is a solution which I use to handle modal.
export default class MyModal extends React.Component<Props, State>{
constructor(props: Props){
super(props);
this.state = {
visible: false,
}
}
// Use this method to toggle the modal !
toggleModal = () => {
this.setState({visible: !this.state.visible});
}
render(){
return(
<Modal
isVisible={this.state.visible}
hideModalContentWhileAnimating
onBackdropPress={() => {
this.toggleModal();
}}
useNativeDriver
>
<View style={{backgroundColor: "white", padding: 5}}>
</View>
</Modal>
);
}
}
If I press behind it, the modal will close -> it can close itself.
Now to manage it from the parent component just get a ref from your modal :
<MyModal
ref={ref => {
this.myModal = ref;
}}
/>
And you can toggle it from the parent component :
toggleMyModal = () => {
if(this.myModal){
this.myModal.toggleModal();
}
}
Let me know if you got it working :)
It worked thanks!
– Oscar Fen
Nov 25 '18 at 15:51
No problem @OscarFen ! Please consider accepting the answer as solution ;)
– Hugo Laplace
Nov 25 '18 at 16:39
I don't have enough reputation :(
– Oscar Fen
Nov 25 '18 at 16:49
You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project
– Hugo Laplace
Nov 25 '18 at 16:53
add a comment |
Here is a solution which I use to handle modal.
export default class MyModal extends React.Component<Props, State>{
constructor(props: Props){
super(props);
this.state = {
visible: false,
}
}
// Use this method to toggle the modal !
toggleModal = () => {
this.setState({visible: !this.state.visible});
}
render(){
return(
<Modal
isVisible={this.state.visible}
hideModalContentWhileAnimating
onBackdropPress={() => {
this.toggleModal();
}}
useNativeDriver
>
<View style={{backgroundColor: "white", padding: 5}}>
</View>
</Modal>
);
}
}
If I press behind it, the modal will close -> it can close itself.
Now to manage it from the parent component just get a ref from your modal :
<MyModal
ref={ref => {
this.myModal = ref;
}}
/>
And you can toggle it from the parent component :
toggleMyModal = () => {
if(this.myModal){
this.myModal.toggleModal();
}
}
Let me know if you got it working :)
Here is a solution which I use to handle modal.
export default class MyModal extends React.Component<Props, State>{
constructor(props: Props){
super(props);
this.state = {
visible: false,
}
}
// Use this method to toggle the modal !
toggleModal = () => {
this.setState({visible: !this.state.visible});
}
render(){
return(
<Modal
isVisible={this.state.visible}
hideModalContentWhileAnimating
onBackdropPress={() => {
this.toggleModal();
}}
useNativeDriver
>
<View style={{backgroundColor: "white", padding: 5}}>
</View>
</Modal>
);
}
}
If I press behind it, the modal will close -> it can close itself.
Now to manage it from the parent component just get a ref from your modal :
<MyModal
ref={ref => {
this.myModal = ref;
}}
/>
And you can toggle it from the parent component :
toggleMyModal = () => {
if(this.myModal){
this.myModal.toggleModal();
}
}
Let me know if you got it working :)
answered Nov 23 '18 at 22:11
Hugo LaplaceHugo Laplace
1669
1669
It worked thanks!
– Oscar Fen
Nov 25 '18 at 15:51
No problem @OscarFen ! Please consider accepting the answer as solution ;)
– Hugo Laplace
Nov 25 '18 at 16:39
I don't have enough reputation :(
– Oscar Fen
Nov 25 '18 at 16:49
You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project
– Hugo Laplace
Nov 25 '18 at 16:53
add a comment |
It worked thanks!
– Oscar Fen
Nov 25 '18 at 15:51
No problem @OscarFen ! Please consider accepting the answer as solution ;)
– Hugo Laplace
Nov 25 '18 at 16:39
I don't have enough reputation :(
– Oscar Fen
Nov 25 '18 at 16:49
You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project
– Hugo Laplace
Nov 25 '18 at 16:53
It worked thanks!
– Oscar Fen
Nov 25 '18 at 15:51
It worked thanks!
– Oscar Fen
Nov 25 '18 at 15:51
No problem @OscarFen ! Please consider accepting the answer as solution ;)
– Hugo Laplace
Nov 25 '18 at 16:39
No problem @OscarFen ! Please consider accepting the answer as solution ;)
– Hugo Laplace
Nov 25 '18 at 16:39
I don't have enough reputation :(
– Oscar Fen
Nov 25 '18 at 16:49
I don't have enough reputation :(
– Oscar Fen
Nov 25 '18 at 16:49
You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project
– Hugo Laplace
Nov 25 '18 at 16:53
You can't click on the little "check" sign under the answer score ? Strange that you can't accept a solution on your own question ! Anyway good luck with your project
– Hugo Laplace
Nov 25 '18 at 16:53
add a comment |
ModalAddTrip.js
_toggleModal = () =>
this.setState({ isVisible: !this.state.isVisible });
....
render() {
....
<Modal
isVisible={isVisible}
onBackdropPress={onBackDropPress}
>
<Button
style={{ fontSize: 18, color: 'white' }}
containerStyle={{
padding: 8,
marginLeft: 70,
}}
onPress={this._toggleModal} >
</Modal>
didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?
– Oscar Fen
Nov 23 '18 at 17:42
add a comment |
ModalAddTrip.js
_toggleModal = () =>
this.setState({ isVisible: !this.state.isVisible });
....
render() {
....
<Modal
isVisible={isVisible}
onBackdropPress={onBackDropPress}
>
<Button
style={{ fontSize: 18, color: 'white' }}
containerStyle={{
padding: 8,
marginLeft: 70,
}}
onPress={this._toggleModal} >
</Modal>
didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?
– Oscar Fen
Nov 23 '18 at 17:42
add a comment |
ModalAddTrip.js
_toggleModal = () =>
this.setState({ isVisible: !this.state.isVisible });
....
render() {
....
<Modal
isVisible={isVisible}
onBackdropPress={onBackDropPress}
>
<Button
style={{ fontSize: 18, color: 'white' }}
containerStyle={{
padding: 8,
marginLeft: 70,
}}
onPress={this._toggleModal} >
</Modal>
ModalAddTrip.js
_toggleModal = () =>
this.setState({ isVisible: !this.state.isVisible });
....
render() {
....
<Modal
isVisible={isVisible}
onBackdropPress={onBackDropPress}
>
<Button
style={{ fontSize: 18, color: 'white' }}
containerStyle={{
padding: 8,
marginLeft: 70,
}}
onPress={this._toggleModal} >
</Modal>
answered Nov 23 '18 at 16:21
Selmi KarimSelmi Karim
532110
532110
didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?
– Oscar Fen
Nov 23 '18 at 17:42
add a comment |
didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?
– Oscar Fen
Nov 23 '18 at 17:42
didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?
– Oscar Fen
Nov 23 '18 at 17:42
didn't work, thanks for you help though. EDIT: Could it be something do with my state in ModalAddTrip?
– Oscar Fen
Nov 23 '18 at 17:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449752%2fclosing-modal-in-child-component-react-native%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown