react-native - navigate to another screen using react native
问题描述
I am trying to navigate to another screen while pressing on a button using onpress in react native but getting error. Front page is App.js which contains multiple buttons, I am working on event button for now. when I press on event button then it should redirect to the contain in event.js I am a newbie in react. App.js
import React from "react";
import {StyleSheet,Text,View,Button,Image,ProgressViewIOS} from "react-native";
import event from './event.js';
export default class App extends React.Component {
render() {
return (
<View>
<Text style={styles.h1}>WELCOME TO </Text>
<Image
style={styles.logo}
source={require('./logo_tiny.jpg')}
/>
<View style={styles.container}>
<View style={styles.b1}>
<Button
title="Events"
onPress={() => this.onPress('navigateToevent')}
color="#fff"
/>
</View>
<View style={styles.b1}>
<Button
title="Package"
onPress={() => this.onPress()}
color="#fff"
/>
</View>
</View>
<View style={styles.container}>
<View style={styles.b1}>
<Button
title="Promotion"
onPress={() => this.onPress()}
color="#fff"
/>
</View>
<View style={styles.b1}>
<Button
title="Support"
onPress={() => this.onPress()}
color="#fff"
/>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
h1: {
textAlign: 'center',
color: '#1E7778',
fontSize: 32,
marginTop: 18,
width: 400,
fontWeight: 'bold',
},
container: {
flexDirection: 'row',
},
b1: {
width: '40%',
margin: 20,
padding: 10,
backgroundColor: '#D16B11',
borderRadius: 20,
},
logo: {
alignItems: 'center',
justifyContent:'center',
left: '20%',
},
});
event.js
import React from 'react';
export default class event extends React.Component {
navigateToevent = () => {
this.props.navigation.navigate('event');
};
constructor(props) {
super(props);
this.state = {
country: null,
city: null,
cities: []
};
}
changeCountry(item) {
let city = null;
let cities;
switch (item.value) {
case 'fr':
cities = [
{label: 'Paris', value: 'paris'}
];
break;
case 'es':
cities = [
{label: 'Madrid', value: 'madrid'}
];
break;
}
this.setState({
city,
cities
});
}
changeCity(item) {
this.setState({
city: item.value
});
}
render() {
return (
<>
<DropDownPicker
items={[
{label: 'France', value: 'fr'},
{label: 'Spain', value: 'es'},
]}
defaultNull
placeholder="Select your country"
containerStyle={{height: 40}}
onChangeItem={item => this.changeCountry(item)}
/>
<DropDownPicker
items={this.state.cities}
defaultNull={this.state.city === null}
placeholder="Select your city"
containerStyle={{height: 40}}
onChangeItem={item => this.changeCity(item)}
/>
</>
);
}
}
解决方案
// event.js
import React from "react";
import DropDownPicker from "react-native-dropdown-picker";
export default class event extends React.Component {
constructor(props) {
super(props);
this.state = {
country: null,
city: null,
cities: [],
};
}
changeCountry(item) {
let city = null;
let cities;
switch (item.value) {
case "fr":
cities = [{ label: "Paris", value: "paris" }];
break;
case "es":
cities = [{ label: "Madrid", value: "madrid" }];
break;
}
this.setState({
city,
cities,
});
}
changeCity(item) {
this.setState({
city: item.value,
});
}
render() {
return (
<>
<DropDownPicker
items={[
{ label: "France", value: "fr" },
{ label: "Spain", value: "es" },
]}
defaultNull
placeholder="Select your country"
containerStyle={{ height: 40 }}
onChangeItem={(item) => this.changeCountry(item)}
/>
<DropDownPicker
items={this.state.cities}
defaultNull={this.state.city === null}
placeholder="Select your city"
containerStyle={{ height: 40 }}
onChangeItem={(item) => this.changeCity(item)}
/>
</>
);
}
}
// main.js
import React from "react";
import {
StyleSheet,
Text,
View,
Button,
Image,
ProgressViewIOS,
} from "react-native";
import event from "./event.js";
export default class Main extends React.Component {
navigateToevent = () => {
this.props.navigation.navigate("event");
};
render() {
return (
<View>
<Text style={styles.h1}>WELCOME TO </Text>
<Image style={styles.logo} />
<View style={styles.container}>
<View style={styles.b1}>
<Button
title="Events"
onPress={() => this.navigateToevent()}
color="#fff"
/>
</View>
<View style={styles.b1}>
<Button
title="Package"
onPress={() => this.onPress()}
color="#fff"
/>
</View>
</View>
<View style={styles.container}>
<View style={styles.b1}>
<Button
title="Promotion"
onPress={() => this.onPress()}
color="#fff"
/>
</View>
<View style={styles.b1}>
<Button
title="Support"
onPress={() => this.onPress()}
color="#fff"
/>
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
h1: {
textAlign: "center",
color: "#1E7778",
fontSize: 32,
marginTop: 18,
width: 400,
fontWeight: "bold",
},
container: {
flexDirection: "row",
},
b1: {
width: "40%",
margin: 20,
padding: 10,
backgroundColor: "#D16B11",
borderRadius: 20,
},
logo: {
alignItems: "center",
justifyContent: "center",
left: "20%",
},
});
// App.js
import React from "react";
import {
StyleSheet,
Text,
View,
Button,
Image,
ProgressViewIOS,
} from "react-native";
import event from "./event.js";
import main from "./main.js";
import { createAppContainer } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";
const AppNavigator = createStackNavigator({
main: {
screen: main,
},
event: {
screen: event,
},
});
class App extends React.Component {
render() {
return (
<View>
<AppNavigator />
</View>
);
}
}
export default AppContainer = createAppContainer(AppNavigator);
试试这个对你有用
推荐阅读
- vba - 将变量从模块传递到用户表单
- flutter - FutureBuilder 块空问题
- javascript - 如何在 NodeJS 中将 fetch 导入我的 app.js?
- react-native - 如何用 Jest 测试 StackActions?
- java - 查询 DynamoDb 全局二级索引
- scala - Spray-json: [错误] 引用不存在的类 spray.json.package$
- verilog - 如何实现非连续采样点的过渡覆盖?
- image - Azure DevOps 配置文件图片未显示在板上
- android - 无法迁移到 Gradle DSL 插件,找不到“com.android.library”]
- c# - 如何在 .Net MVC 中使用 Array 的内容填充 DropDown?