React-Native封装一些模块
2017-03-28 19:06
387 查看
存储
http://www.jianshu.com/p/f9965c6feceb
react-native-root-toast使用
/** *ToastUtil.js */ import Toast from 'react-native-root-toast'; let toast; export const toastShort = (content) => { if (toast !== undefined) { Toast.hide(toast); } toast = Toast.show(content.toString(), { duration: Toast.durations.SHORT, position: Toast.positions.CENTER, shadow: true, animation: true, hideOnPress: true, delay: 0 }); }; export const toastLong = (content) => { if (toast !== undefined) { Toast.hide(toast); } toast = Toast.show(content.toString(), { duration: Toast.durations.LONG, position: Toast.positions.BOTTOM, shadow: true, animation: true, hideOnPress: true, delay: 0 }); };
import { toastShort } from '../common/ToastUtil'; toastShort('已收藏');
存储
import React from 'react-native'; const { AsyncStorage } = React; class DeviceStorage { static get(key) { return AsyncStorage.getItem(key).then((value) => { const jsonValue = JSON.parse(value); return jsonValue; }); } static save(key, value) { return AsyncStorage.setItem(key, JSON.stringify(value)); } static update(key, value) { return DeviceStorage.get(key).then((item) => { value = typeof value === 'string' ? value : Object.assign({}, item, value); return AsyncStorage.setItem(key, JSON.stringify(value)); }); } static delete(key) { return AsyncStorage.removeItem(key); } } export default DeviceStorage;
更新
import CodePush from 'react-native-code-push'; componentDidMount() { CodePush.sync({ deploymentKey: 'lCYb0hSXQUEJHWiSykloC7wXu_19V1dAekTcW', updateDialog: { optionalIgnoreButtonLabel: '稍后', optionalInstallButtonLabel: '后台更新', optionalUpdateMessage: '“ ”有新版本了,是否更新?', title: '更新提示', }, installMode: CodePush.InstallMode.ON_NEXT_RESTART, }); }
宽度高度
import {Dimensions} from 'react-native'; let window = { width: Dimensions.get('window').width, height: Dimensions.get('window').height, } export default { window: window, }
路由
import React, { Component } from 'react' import { Navigator, Platform, View } from 'react-native' import IndexView from '../Index/Index' export default class NavigatorComp extends Component { render() { return ( <View style={styles.container}> <Navigator initialRoute={{name: 'indexView',component:IndexView}} configureScene={this._configureScene} renderScene={this._renderScene} /> </View> ) } _renderScene(route, navigator) { return <route.component navigator={navigator} {...route.params} />; } _configureScene(route, routeStack) { switch (route.type) { case 'floatFromBottom': return Navigator.SceneConfigs.FloatFromBottom default: return Navigator.SceneConfigs.PushFromRight } } } const styles = { container: { flexGrow: 1 } }
首页
import React, {Component} from 'react'; import { View, BackAndroid, ToastAndroid } from 'react-native'; import Main from '../Main/Main'; import NavbarComp from '../Navigator/navigator' export default class IndexView extends Component { //注册Android环境物理返回监听事件 componentWillMount(){ BackAndroid.addEventListener('hardwareBackPress', this.onBackAndroid); } //解绑Android环境物理返回监听事件 componentWillUnmount() { BackAndroid.removeEventListener('hardwareBackPress', this.onBackAndroid); } //Android物理返回键处理 onBackAndroid = () => { const routers = this.props.navigator.getCurrentRoutes(); // 当前页面不为root页面时的处理 if (routers.length > 1) { const top = routers[routers.length - 1]; if (top.ignoreBack || top.component.ignoreBack) { // 路由或组件上决定这个界面忽略back键 return true; } const handleBack = top.handleBack || top.component.handleBack; if (handleBack) { // 路由或组件上决定这个界面自行处理back键 return handleBack(); } // 默认行为: 退出当前界面。 navigator.pop(); return true; } if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) { //最近2秒内按过back键,可以退出应用。 return false; } this.lastBackPressed = Date.now(); ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT); return true; }; render() { return ( <View style={{flexGrow: 1}}> <Main navigator={this.props.navigator}/> </View> ); } }
相关文章推荐
- [置顶] React-Native开发之原生模块封装(Android)升级版
- React Native开源特效动画封装库模块(lottie-react-native)
- React-Native开发之原生模块封装(Android)升级版
- React Native开源封装AES,MD5加密模块(react-native-encryption-library)
- react native学习笔记25——Android原生模块的封装与调用
- React-Native 自封装原生模块
- React Native调用Android原生模块
- Learning ReactNative (一) : JavaScript模块基本原理与用法
- react-native组件传值与封装
- react-native组件封装与传值
- React Native添加Android原生模块
- React-Native中遇到的一些坑(未完待续)
- ReactNative之原生模块开发并发布--iOS篇
- Android React Native使用原生模块
- react-native的一些简单命令(怕忘记,持续更新)
- 《React-Native系列》12、 API模块之PixelRatio和Dimensions
- React Native之原生UI组件封装---适配Android
- 一些React-Native学习资料
- Android之React Native平台与Android本地模块之间的调用
- React-Native 原生模块(iOS)调用