react 怎样跳转到另一个页面
跳转的主要方法:
1.component 中添加这行代码
<View style={styles.loginmain}>
<Text style={styles.logintext} onPress={() => navigator.push({name:'In'})}>注册</Text>
<Text style={styles.logintext} onPress={() => navigator.push({name:'Forget'})}>忘记密码 </Text>
</View>
onPress 主要运用于点击事件中。
2.在运行的主页面中只能运行如下的component
const thunkMiddleWare = (store) => (next) => (action) => {
if (typeof action === 'function') {
return action(store.dispatch, store.getState)
}
return next(action)
export default function () {
return (
<Provider store={createStore(reducer, applyMiddleware(thunkMiddleWare))}>
<NavigatorApp />
</Provider>
)
}
需要注意的是:a. middleware 是中间件的设置,它有固定的格式.
<view/> 不能包含<Navigator/>这个标签 但反过来可以。
3.点击跳转的页面的设置代码
function InComponent({navigator}){
return (
<View style={[styles.fullCenter,{backgroundColor:'#CCC',flex:1}]}>
<Text style={styles.size} onPress={() => navigator.pop()} >注册</Text>
</View>
)
}
function ForgetComponent({navigator}){
return (
<View style={[styles.fullCenter,{backgroundColor:'#CCC',flex:1}]}>
<Text style={styles.size} onPress={() => navigator.pop()} >忘记密码</Text>
</View>
)
}
export default class NavigatorApp extends Component {
render() {
return (
<Navigator
initialRoute={{name:'Main'}}
renderScene={this.renderScene}
navigationBar ={this.navigationBar}
/>
);
}
renderScene(route,navigator){
if (route.name==="Main"){
return <App navigator={navigator}/>
}
if (route.name==="In"){
return <InComponent navigator={navigator}/>
}
if (route.name==="Forget"){
return <ForgetComponent navigator={navigator}/>
}
if (route.name==='Nav'){
return <NavComponent navigator={navigator} />
}
}
// configureScene (route,navigator) {
// return Navigator.SceneConfigs.FloatFromBottom
// }
总结:
ReactNative结合了Web应用和Native应用的优势,可以使用JavaScript来开发iOS和Android原生应用。在JavaScript中用React抽象操作系统原生的UI组件,代替DOM元素来渲染等。
<View style={styles.loginmain}>
<Text style={styles.logintext} onPress={() => navigator.push({name:'In'})}>注册</Text>
<Text style={styles.logintext} onPress={() => navigator.push({name:'Forget'})}>忘记密码 </Text>
</View>
onPress 主要运用于点击事件中
2.在运行的主页面中只能运行如下的component
const thunkMiddleWare = (store) => (next) => (action) => {
if (typeof action === 'function') {
return action(store.dispatch, store.getState)
}
return next(action)
export default function () {
return (
<Provider store={createStore(reducer, applyMiddleware(thunkMiddleWare))}>
<NavigatorApp />
</Provider>
)
}
需要注意的是:a. middleware 是中间件的设置,它有固定的格式.
<view/> 不能包含<Navigator/>这个标签 但反过来可以
3.点击跳转的页面的设置代码
function InComponent({navigator}){
return (
<View style={[styles.fullCenter,{backgroundColor:'#CCC',flex:1}]}>
<Text style={styles.size} onPress={() => navigator.pop()} >注册</Text>
</View>
)
}
function ForgetComponent({navigator}){
return (
<View style={[styles.fullCenter,{backgroundColor:'#CCC',flex:1}]}>
<Text style={styles.size} onPress={() => navigator.pop()} >忘记密码</Text>
</View>
)
}
export default class NavigatorApp extends Component {
render() {
return (
<Navigator
initialRoute={{name:'Main'}}
renderScene={this.renderScene}
navigationBar ={this.navigationBar}
/>
);
}
renderScene(route,navigator){
if (route.name==="Main"){
return <App navigator={navigator}/>
}
if (route.name==="In"){
return <InComponent navigator={navigator}/>
}
if (route.name==="Forget"){
return <ForgetComponent navigator={navigator}/>
}
if (route.name==='Nav'){
return <NavComponent navigator={navigator} />
}
}
// configureScene (route,navigator) {
// return Navigator.SceneConfigs.FloatFromBottom
// }