@imranmungai/react-native-expo-fancy-alerts
灵感来源于 Nathan Walker 的 nativescript-fancyalert - 一个简单、基本的实现,将库提供的美丽警报功能呈现出来
npm i --save @imranmungai/react-native-expo-fancy-alerts
- 版本:2.1.2
- GitHub: https://github.com/vincent95/react-native-expo-fancy-alerts
- NPM: https://npmjs.net.cn/package/%40imranmungai%2Freact-native-expo-fancy-alerts
- 下载量
- 昨日:1
- 上周:20
- 上个月:72
React Native Fancy Alerts
对 nativescript-fancyalert 的 react native 版本。与 expo 兼容 🤓
![]() |
![]() |
![]() |
---|
快速开始
$ 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;
参考
LoadingIndicator
属性 | 类型 | 必需 | 默认 | 描述 |
---|---|---|---|---|
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 类型定义