在移动端,Toast方式提示消息更为普遍。

1.安装插件react-native-easy-toast
github地址:https://github.com/crazycodeboy/react-native-easy-toast
中文文档:https://github.com/crazycodeboy/react-native-easy-toast/blob/master/README.zh.md
$ npm install react-native-easy-toast --save
2.引入react-native-easy-toast
import Toast from 'react-native-easy-toast';
3.在最外层View底部渲染Toast
<View style={[{flex: 1}]}>
{/*<Router sceneStyle={[styles.router]}>*/}
{/*......*/}
{/*</Router>*/}
{/*<PlayButton />*/}
<Toast ref="toast" opacity={0.8}/>
{/*<Loading ref={r=>{this.Loading = r}} hide = {true} />*/}
</View>
4.定义全局toast方法
let self; //将App组件中的this赋给全局的self
global.toast = false; //所有子页面均可直接调用global.toast("")来吐司提示消息
5.给全局方法赋值
componentDidMount() {
self = this;
global.toast = function(message) {
self.refs.toast.show(message);
};
}
6.调用
global.toast('这是消息提示');

本文介绍如何在移动端使用react-native-easy-toast插件实现Toast消息提示功能。包括安装配置、全局方法定义及调用示例。
852





