-
Notifications
You must be signed in to change notification settings - Fork 613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
multiple modals not working #171
Comments
Hi! There should be a few GitHub issues both here and in the react-native repo discussing the subject. It happens even with dialogs/alerts. If, instead of showing the modals at the same time, you just want to show a modal one after another you should keep in mind that you should wait for the animation to complete before showing it (both with this modal with the original one). TLDR: Let me know if you need further info! |
hey @mmazzarolo thanks for your answer. i think you misunderstand my issue, so let me explain:
|
Re-opening, sorry if I misunderstood it.
Oh! I'm was not aware of this -- and I don't know if I understood it correctly, could you show me a snippet? Are those modals animated?
Well, this is actually is a known issue of react-native about modals, alerts and dialogs. |
Closing, feel free to write here again if you want to discuss it further and I'll re-open it 👍 |
@macrozone Hey, i was facing a similar issue and i wrote a wrapper on top of the react-native-modal that does exactly what you said. If one modal is already visible and another one needs to show itself, then the wrapper will close the first modal and show the second one automatically. The UI won't freeze and stop working. |
hi guys, I switched back to the native modal for the time beeing, but i might revisit this one here in the future. @kalwaniyash if you have a link to a repo or gist, do you mind sharing it? thank you! |
@macrozone the code currently is deeply integrated with my project i will soon create a public repo and share it over here. |
Re-opening 👍 |
@kalwaniyash what approach did you use. |
@kalwaniyash I'm interested as well, can you tell us more? |
@jojonarte @IlifilzaRusli Hey, the approach that i used is this. Sorry for the delay, by this weekend i'll be making the entire code public. |
Multiple modals might work using the |
mine worked by using onModalHide then setTimeout on the setState of the next modal |
@jojonarte @IlifilzaRusli |
@kalwaniyash Are you going to send this proposal to the main repository? |
@kalwaniyash This solution is gold 👍. I've been fighting with this for ages now. Looking at your github repo though (https://github.com/kalwaniyash/react-native-modal-wrapper), this will be non-trivial to merge into react-native-modal core. It's also likely going to require some refactoring since you're using unsafe methods like |
Closing, you can check the "Multiple Modals" section in the README.md 👍 |
As another workaround, I created a Usage: The name is used as the lock, and if not provided it works as a regular It would be great if this kind of functionality could be implemented in the MutexModal.tsximport React, { useEffect } from 'react';
import Modal, { ModalProps } from 'react-native-modal';
import { commonConstants } from '../styles/layout';
import { RootReducerState } from '../../store/types';
import {
lockVisibleModal as lockVisibleModalAction,
releaseVisibleModal as releaseVisibleModalAction,
} from '../../store/ui/UiActions';
import { connect } from 'react-redux';
import { Dispatch } from 'redux';
interface OwnProps extends ModalProps {
name?: string;
}
interface StateProps {
visibleModal: string | null;
}
interface DispatchProps {
lockVisibleModal: (name: string) => void;
releaseVisibleModal: (name: string) => void;
}
type Props = OwnProps & StateProps & DispatchProps;
/**
* A wrapper for Modal which displays at most one modal at a time. If multiple
* modals are displayed at a time, the latter modals never appear.
* https://github.com/react-native-community/react-native-modal/issues/171
*
* If the 'name' prop is not passed, it works just as Modal.
*/
const MutexModal = ({
isVisible,
name,
visibleModal,
lockVisibleModal,
releaseVisibleModal,
...props
}: Props) => {
useEffect(() => {
if (name) {
if (isVisible && !visibleModal) {
// Grab modal mutex when available
lockVisibleModal(name);
}
if (!isVisible && visibleModal === name) {
// Wait for modal hide animation before releasing mutex
setTimeout(() => releaseVisibleModal(name), commonConstants.delayHideModal);
}
}
}, [isVisible, visibleModal, name, lockVisibleModal, releaseVisibleModal]);
const visible = name ? isVisible && visibleModal === name : isVisible;
return <Modal isVisible={visible} {...props} />;
};
const mapStateToProps = (state: RootReducerState): StateProps => {
const visibleModal = state.ui.visibleModal;
return {
visibleModal,
};
};
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
lockVisibleModal: (modal: string) => dispatch(lockVisibleModalAction(modal)),
releaseVisibleModal: (modal: string) => dispatch(releaseVisibleModalAction(modal)),
});
export default connect(mapStateToProps, mapDispatchToProps)(MutexModal); UiReducer.ts case ActionTypes.LOCK_VISIBLE_MODAL: {
return {
...state,
visibleModal: state.visibleModal ?? action.payload.modal,
};
}
case ActionTypes.RELEASE_VISIBLE_MODAL: {
return {
...state,
visibleModal: state.visibleModal === action.payload.modal ? null : state.visibleModal,
};
} |
I fixed this with conditional rendering: {
|
Just try embed ModalB in ModalA. It will work. <ModalA>
<ModalB/>
</ModalA> |
Flickering happens if using react-native picker inside modal. |
This worked. Thanks, @452MJ . |
absolutely thanks |
i have a similar issue, 1st modal i set to false then 2nd modal i set to true sequentially.
|
You can use setTimeout instead of runAfterInteractions, it works but is not a perfect solution I also want to know what is the best way to switch between two different modals. |
No, in most cases, the best way is to use InteractionManager. |
I switched from react-native's default Modal to react-native-modal and i noticed that if I have multiple modals open at the same time, only one will ever be visible.
react-native's modal handles this correctly: they display only the first one, but if i dismiss, i see the other modals.
With react-native-modal, the others are simply ignored, altough they are mounted with
isVisible={true}
. This leads to inconsistency.The text was updated successfully, but these errors were encountered: