react-native-expo-fancy-alerts
灵感来自Nathan Walker的nativescript-fancyalert - 美丽的警报库的简单基本实现
npm i --save react-native-expo-fancy-alerts

React Native Fancy Alerts

NPM version Downloads License

为react native适配nativescript-fancyalert。兼容expo 🤓

Screenshot loading Screenshot success Screenshot error

快速开始

$ npm i react-native-expo-fancy-alerts

$ yarn add react-native-expo-fancy-alerts
import React from 'react';
import { Text, TouchableOpacity, View } from 'react-native';
import { FancyAlert } from 'react-native-expo-fancy-alerts';

const App = () => {
const [visible, setVisible] = React.useState(false);
const toggleAlert = React.useCallback(() => {
setVisible(!visible);
}, [visible]);

return (
<View>
<TouchableOpacity onPress={toggleAlert}>
<Text>Tap me</Text>
</TouchableOpacity>

<FancyAlert
visible={visible}
icon={<View style={{
flex: 1,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'red',
borderRadius: 50,
width: '100%',
}}><Text>🤓</Text></View>}
style={{ backgroundColor: 'white' }}
>
<Text style={{ marginTop: -16, marginBottom: 32 }}>Hello there</Text>
</FancyAlert>
</View>
)
}

export default App;

参考

加载指示器

属性 类型 必需 默认 描述
visible bool n/a 是否显示加载指示器

FancyAlert

属性 类型 必需 默认 描述
visible bool n/a 警报是否可见
icon node n/a 警报中要显示的图标
style object {} 类似于任何View中常用的style属性
onRequestClose func () => void 用户点击按钮时要执行的操作
  • 注意 - 点击模糊的背景不会关闭警报

示例

以下示例说明了如何为整个应用程序创建加载指示器。如果您使用redux,您可能有一个存储部分表示是否正在加载某些内容,您可以获取该标志并显示此库提供的加载指示器之一。

import React from 'react';
import { useSelector } from 'react-redux';
import { LoadingIndicator } from 'react-native-expo-fancy-alerts';
import { selectIsLoading } from 'selectors';

const AppLoadingIndicator = () => {
const isLoading = useSelector(selectIsLoading);
return <LoadingIndicator visible={isLoading} />;
}

export default AppLoadingIndicator;

下面是一个通过redux全局管理错误消息的示例。

import React from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import { useDispatch, useSelector } from 'react-redux';
import { FancyAlert } from 'react-native-expo-fancy-alerts';
import { Ionicons } from '@expo/vector-icons';
import { ErrorCreators } from 'creators';
import { selectError } from 'selectors';

const AppErrorModal = () => {
const dispatch = useDispatch();
const { hasError, error } = useSelector(selectError);

const onRequestClose = React.useCallback(
() => {
dispatch(ErrorCreators.hideError());
},
[dispatch],
);

return <FancyAlert
style={styles.alert}
icon={
<View style={[ styles.icon, { borderRadius: 32 } ]}>
<Ionicons
name={Platform.select({ ios: 'ios-close', android: 'md-close' })}
size={36}
color="#FFFFFF"
/>
</View>
}
onRequestClose={onRequestClose}
visible={hasError}
>
<View style={styles.content}>
<Text style={styles.contentText}>{error ? error.message : ''}</Text>

<TouchableOpacity style={styles.btn} onPress={onPress}>
<Text style={styles.btnText}>OK</Text>
</TouchableOpacity>
</View>
</FancyAlert>;
}

const styles = StyleSheet.create({
alert: {
backgroundColor: '#EEEEEE',
},
icon: {
flex: 1,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#C3272B',
width: '100%',
},
content: {
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
marginTop: -16,
marginBottom: 16,
},
contentText: {
textAlign: 'center',
},
btn: {
borderRadius: 32,
display: 'flex',
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
paddingHorizontal: 8,
paddingVertical: 8,
alignSelf: 'stretch',
backgroundColor: '#4CB748',
marginTop: 16,
minWidth: '50%',
paddingHorizontal: 16,
},
btnText: {
color: '#FFFFFF',
},
});

export default AppErrorModal;

变更日志

  • 0.0.1 - 初始实现 - 安卓存在布局问题,将会修复
  • 0.0.2 - 安卓问题已修复
  • 0.0.3 - 添加了额外的自定义选项
  • 1.0.0 - 多年后我决定打包一切并发布 🎉🥳
  • 2.0.0 - 重大变更 更新FancyAlert以使其更直观和通用
  • 2.0.1 - 更新文档以包含一些真实示例
  • 2.0.2 - 更新依赖项
  • 2.1.0 - 添加typescript定义