简体   繁体   English

如何在 react-native-modal 组件willunmount 中完成模态动画?

[英]how to finish modal animation in react-native-modal componentwillunmount?

I am looking for a solution to delay RN component unmount until react-native-modal animationOut is completed.我正在寻找一种解决方案来延迟 RN 组件卸载,直到react-native-modal animationOut完成。 This animation is triggered by setting isVisible=false .这个动画是通过设置isVisible=false触发的。 With my current implementation the animation performs as expected on componentWillMount hook, however the problem is in componentWillUnmount the alert unmounts without an animation.使用我当前的实现,动画在componentWillMount挂钩上按预期执行,但是问题在于componentWillUnmount警报卸载时没有动画。 I suspect this is because the component unmounts before the animation even has a change to start.我怀疑这是因为组件在动画开始之前卸载。 Here's the alert component:这是警报组件:

//modal component
import Modal from "react-native-modal";

export default CustomAlert = props => {

    const [isVisible, setIsVisible] = useState(false);

    useEffect(() => {
        setIsVisible(true)
        return () => setIsVisible(false);
    }, [])

    return (
        <Modal
            isVisible={isVisible}
            animationIn={'slideInUp'}
            animationOut={'slideOutDown'}
        >
            <View
               ...
            </View>
        </Modal>
    )
};

Here's how the custom alert needs to be used:以下是需要使用自定义警报的方式:

export default ApplicationScreen = ({ navigation }) => {

    const [alert, setAlert] = useState(null);

    ...
    const doSomething = () => {
        ...
        //show alert
        setAlert({ title: 'title', message: 'message'});
        ...
        //hide alert
        setAlert(null);
        ...
    }
    ...

    return (
        <SafeAreaView style={styles.container}>

            {alert &&
                <CustomAlert
                    title={alert?.title}
                    message={alert?.message}
                    ...
                />
            }
            ...
        </SafeAreaView>
    )
}

The problem is that you are containing the CustomAlert in a conditional rendering.问题是您在条件渲染中包含CustomAlert Once the alert is set to false, the Modal would dismount directly.一旦警报设置为 false,Modal 将直接下马。 You could pass the alert state into CustomAlert as property and then into Modal .您可以将alert状态作为属性传递给CustomAlert ,然后传递给Modal The 's property isVisible would hanlde the render of the modal for you with animation.的属性isVisible将使用动画为您处理模态的渲染。

ApplicationScreen:应用画面:

<SafeAreaView style={styles.container}>
  <CustomAlert
    title={alert?.title}
    message={alert?.message}
    isAlertVisible={alert? true : false}
  />
</SafeAreaView>

in CustomAlert:在自定义警报中:

return (
        <Modal
            isVisible={props.isAlertVisible}
            animationIn={'slideInUp'}
            animationOut={'slideOutDown'}
        >
            <View
               ...
            </View>
        </Modal>
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM