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

发布于 2025-01-17 01:41:36 字数 1528 浏览 2 评论 0原文

我正在寻找一种解决方案来延迟 RN 组件卸载,直到 react-native-modal animationOut 完成。该动画通过设置 isVisible=false 来触发。 在我当前的实现中,动画在 componentWillMount 挂钩上按预期执行,但问题出在 componentWillUnmount 中,警报在没有动画的情况下卸载。我怀疑这是因为组件在动画发生更改之前就已卸载。 这是警报组件:

//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>
    )
};

以下是自定义警报的使用方式:

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

I am looking for a solution to delay RN component unmount until react-native-modal animationOut is completed. This animation is triggered by setting 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. 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>
    )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少年亿悲伤 2025-01-24 01:41:37

问题是您在条件渲染中包含 CustomAlert。一旦警报设置为 false,Modal 将直接下马。
您可以将 alert 状态作为属性传递到 CustomAlert 中,然后传递到 Modal 中。的属性 isVisible 将为您处理带有动画的模态渲染。

应用程序屏幕:

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

在自定义警报中:

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

The problem is that you are containing the CustomAlert in a conditional rendering. Once the alert is set to false, the Modal would dismount directly.
You could pass the alert state into CustomAlert as property and then into Modal. The 's property isVisible would hanlde the render of the modal for you with animation.

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>
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文