react native Navigator
阿新 • • 發佈:2018-11-03
/** * 導航器元件例項 * https://github.com/facebook/react-native */ 'use strict'; import React, { AppRegistry, Component, StyleSheet, Text, View, TouchableHighlight, Navigator, } from 'react-native'; class NavButton extends React.Component { render() { return ( <TouchableHighlight style={styles.button} underlayColor="#B5B5B5" onPress={this.props.onPress}> <Text style={styles.buttonText}>{this.props.text}</Text> </TouchableHighlight> ); } } class NavMenu extends React.Component { render() { return ( <View style={styles.scene}> <Text style={styles.messageText}>{this.props.message}</Text> <NavButton onPress={() => { this.props.navigator.push({ message: '向右拖拽關閉頁面', sceneConfig: Navigator.SceneConfigs.FloatFromRight, }); }} text="從右邊向左切入頁面(帶有透明度變化)" /> <NavButton onPress={() => { this.props.navigator.push({ message: '向下拖拽關閉頁面', sceneConfig: Navigator.SceneConfigs.FloatFromBottom, }); }} text="從下往上切入頁面(帶有透明度變化)" /> <NavButton onPress={() => { this.props.navigator.pop(); }} text="頁面彈出(回退一頁)" /> <NavButton onPress={() => { this.props.navigator.popToTop(); }} text="頁面彈出(回退到最後一頁)" /> </View> ); } } class NavigatorDemo extends Component { render() { return ( <Navigator style={styles.container} initialRoute={{ message: '初始頁面', }} renderScene={ (route, navigator) => <NavMenu message={route.message} navigator={navigator} />} configureScene={(route) => { if (route.sceneConfig) { return route.sceneConfig; } return Navigator.SceneConfigs.FloatFromBottom; }} /> ); } } const styles = StyleSheet.create({ container: { flex: 1, }, messageText: { fontSize: 17, fontWeight: '500', padding: 15, marginTop: 50, marginLeft: 15, }, button: { backgroundColor: 'white', padding: 15, borderBottomWidth: StyleSheet.hairlineWidth, borderBottomColor: '#CDCDCD', }, }); AppRegistry.registerComponent('NavigatorDemo', () => NavigatorDemo);
執行報錯,navigator is deprecated and has been removed。
開啟當前目錄,
npm install react-native-deprecated-custom-components --save
import {Navigator} from 'react-native-deprecated-custom-components';
修改後程式碼如下
'use strict'; import React, {Component} from 'react'; import { AppRegistry, StyleSheet, Text, View, TouchableHighlight, } from 'react-native'; import {Navigator} from 'react-native-deprecated-custom-components'; class NavButton extends Component { render() { return ( <TouchableHighlight style={styles.button} underlayColor="#B5B5B5" onPress={this.props.onPress}> <Text style={styles.buttonText}>{this.props.text}</Text> </TouchableHighlight> ); } } class NavMenu extends React.Component { render() { return ( <View style={styles.scene}> <Text style={styles.messageText}>{this.props.message}</Text> <NavButton onPress={() => { this.props.navigator.push({ message: '向右拖拽關閉頁面', sceneConfig: Navigator.SceneConfigs.FloatFromRight, }); }} text="從右邊向左切入頁面(帶有透明度變化)" /> <NavButton onPress={() => { this.props.navigator.push({ message: '向下拖拽關閉頁面', sceneConfig: Navigator.SceneConfigs.FloatFromBottom, }); }} text="從下往上切入頁面(帶有透明度變化)" /> <NavButton onPress={() => { this.props.navigator.pop(); }} text="頁面彈出(回退一頁)" /> <NavButton onPress={() => { this.props.navigator.popToTop(); }} text="頁面彈出(回退到最後一頁)" /> </View> ); } } class NavigatorDemo extends Component { render() { return ( <Navigator style={styles.container} initialRoute={{ message: '初始頁面', }} renderScene={ (route, navigator) => <NavMenu message={route.message} navigator={navigator} />} configureScene={(route) => { if (route.sceneConfig) { return route.sceneConfig; } return Navigator.SceneConfigs.FloatFromBottom; }} /> ); } } const styles = StyleSheet.create({ container: { flex: 1, }, messageText: { fontSize: 17, fontWeight: '500', padding: 15, marginTop: 50, marginLeft: 15, }, button: { backgroundColor: 'white', padding: 15, borderBottomWidth: StyleSheet.hairlineWidth, borderBottomColor: '#CDCDCD', }, }); AppRegistry.registerComponent('testrn', () => NavigatorDemo);