React native 如何将字符串从一个组件传递到另一个组件?
登录组件:React native 如何将字符串从一个组件传递到另一个组件?,react-native,React Native,登录组件: import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, TouchableHighlight, TextInput, Image } from 'react-native'; var DisplayData = require('./DisplayData'); export default class Login extends Co
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
TextInput,
Image
} from 'react-native';
var DisplayData = require('./DisplayData');
export default class Login extends Component{
constructor(props){
super(props)
this.state = {
Latitude: '',
Longitude: '',
}
}
onPressDisplayData() {
this.props.navigator.push({
name: 'DisplayView',
component: DisplayData
});
}
render() {
return (
<View style = {styles.container}>
<Image source={require('./logo.png')}/>
<TextInput
style = {styles.input}
placeholder = 'Latitude'
autoCapitalize = 'none'
onChangeText={(text) => this.setState({Latitude:text})}
/>
<TextInput
style = {styles.input}
placeholder = 'Longitude'
autoCapitalize = 'none'
onChangeText={(text) => this.setState({Longitude:text})}
/>
<TouchableHighlight
style = {styles.submit}
onPress = {() => this.onPressDisplayData()
}
>
<Text>
Submit
</Text>
</TouchableHighlight>
</View>
);
}
}
const styles = StyleSheet.create ({
container: {
flex: 1,
alignItems: 'center',
justifyContent:'center',
paddingBottom: 40
},
input: {
margin: 15,
height: 40,
borderColor: 'grey',
borderWidth: 1
},
submit: {
backgroundColor: '#FFDD03',
padding: 10
}
})
module.exports = Login;
import React, {
Component,
} from 'react';
import {
AppRegistry,
Image,
ListView,
StyleSheet,
Text,
View,
} from 'react-native';
var REQUEST_URL = 'http://api.geonames.org/earthquakesJSON?north='+4+'&south='+-9.9+'&east='+-22.4+'&west='+55.2+'&username=afdsanfd'
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
class DisplayData extends Component {
constructor(props){
super(props)
this.state = {
earthquakes: ds.cloneWithRows([])
};
}
componentDidMount() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => this.setState({ earthquakes: ds.cloneWithRows(responseData.earthquakes) }))
.catch(function(error) {
console.warn(error);
});
}
render() {
return (
<View>
<ListView
dataSource = {this.state.earthquakes}
renderRow={(rowData) => (
<View style={{flex: 1, paddingTop: 10, paddingBottom: 10, borderWidth: 0.5, paddingLeft: 10, borderColor: '#D3D3D3'}}>
<Text style={{fontSize: 25}}>{rowData.src}</Text>
<Text>DateTime: {rowData.datetime}</Text>
<Text>Magnitude: {rowData.magnitude}</Text>
<Text>EqID: {rowData.eqid}</Text>
<Text>Depth: {rowData.depth}</Text>
</View>
)}
/>
</View>
);
}
}
module.exports = DisplayData;
import React,{Component}来自'React';
进口{
评估学,
样式表,
文本,
看法
触控高光,
文本输入,
形象
}从“反应本机”;
var DisplayData=require('./DisplayData');
导出默认类登录扩展组件{
建造师(道具){
超级(道具)
此.state={
纬度:'',
经度:'',
}
}
onPressDisplayData(){
这个是.props.navigator.push({
名称:“显示视图”,
组件:显示数据
});
}
render(){
返回(
this.setState({Latitude:text})
/>
this.setState({longide:text})
/>
此文件名为.onPressDisplayData()
}
>
提交
);
}
}
const styles=StyleSheet.create({
容器:{
弹性:1,
对齐项目:“居中”,
辩护内容:'中心',
垫底:40
},
输入:{
差额:15,
身高:40,
边框颜色:“灰色”,
边框宽度:1
},
提交:{
背景颜色:“#FFDD03”,
填充:10
}
})
module.exports=登录;
显示数据组件:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
TextInput,
Image
} from 'react-native';
var DisplayData = require('./DisplayData');
export default class Login extends Component{
constructor(props){
super(props)
this.state = {
Latitude: '',
Longitude: '',
}
}
onPressDisplayData() {
this.props.navigator.push({
name: 'DisplayView',
component: DisplayData
});
}
render() {
return (
<View style = {styles.container}>
<Image source={require('./logo.png')}/>
<TextInput
style = {styles.input}
placeholder = 'Latitude'
autoCapitalize = 'none'
onChangeText={(text) => this.setState({Latitude:text})}
/>
<TextInput
style = {styles.input}
placeholder = 'Longitude'
autoCapitalize = 'none'
onChangeText={(text) => this.setState({Longitude:text})}
/>
<TouchableHighlight
style = {styles.submit}
onPress = {() => this.onPressDisplayData()
}
>
<Text>
Submit
</Text>
</TouchableHighlight>
</View>
);
}
}
const styles = StyleSheet.create ({
container: {
flex: 1,
alignItems: 'center',
justifyContent:'center',
paddingBottom: 40
},
input: {
margin: 15,
height: 40,
borderColor: 'grey',
borderWidth: 1
},
submit: {
backgroundColor: '#FFDD03',
padding: 10
}
})
module.exports = Login;
import React, {
Component,
} from 'react';
import {
AppRegistry,
Image,
ListView,
StyleSheet,
Text,
View,
} from 'react-native';
var REQUEST_URL = 'http://api.geonames.org/earthquakesJSON?north='+4+'&south='+-9.9+'&east='+-22.4+'&west='+55.2+'&username=afdsanfd'
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
class DisplayData extends Component {
constructor(props){
super(props)
this.state = {
earthquakes: ds.cloneWithRows([])
};
}
componentDidMount() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => this.setState({ earthquakes: ds.cloneWithRows(responseData.earthquakes) }))
.catch(function(error) {
console.warn(error);
});
}
render() {
return (
<View>
<ListView
dataSource = {this.state.earthquakes}
renderRow={(rowData) => (
<View style={{flex: 1, paddingTop: 10, paddingBottom: 10, borderWidth: 0.5, paddingLeft: 10, borderColor: '#D3D3D3'}}>
<Text style={{fontSize: 25}}>{rowData.src}</Text>
<Text>DateTime: {rowData.datetime}</Text>
<Text>Magnitude: {rowData.magnitude}</Text>
<Text>EqID: {rowData.eqid}</Text>
<Text>Depth: {rowData.depth}</Text>
</View>
)}
/>
</View>
);
}
}
module.exports = DisplayData;
import-React{
组成部分,
}从"反应",;
进口{
评估学,
形象,,
ListView,
样式表,
文本,
看法
}从“反应本机”;
var请求http://api.geonames.org/earthquakesJSON?north=“+4+”&南部=“+-9.9+”&东部=“+-22.4+”&西部=“+55.2+”&用户名=afdsanfd”
const ds=new ListView.DataSource({rowHasChanged:(r1,r2)=>r1!==r2});
类DisplayData扩展组件{
建造师(道具){
超级(道具)
此.state={
地震:ds.cloneWithRows([])
};
}
componentDidMount(){
获取(请求URL)
.then((response)=>response.json())
.then((responseData)=>this.setState({地震:ds.cloneWithRows(responseData.地震)}))
.catch(函数(错误){
控制台。警告(错误);
});
}
render(){
返回(
(
{rowData.src}
日期时间:{rowData.DateTime}
震级:{rowData.magnity}
EqID:{rowData.EqID}
深度:{rowData.Depth}
)}
/>
);
}
}
module.exports=显示数据;
所以我试图通过这个应用程序实现的是让某人输入纬度和经度,这样一个API调用(请求URL)就会返回地震信息。然而,我正在努力将输入的纬度和经度传递到DisplayData组件中。我尝试过创建全球纬度和经度等方法,但都没有成功 您应该使用navigator发送数据。发送和接收的方式取决于您使用的导航器。您正在使用哪个导航器?是的,如@meysamIzadmehr所述,您可以指定是导航器还是普通导航器(检查导航器的第一个调用位置)您应该使用导航器发送数据的可能重复项。发送和接收的方式取决于您使用的导航器。您正在使用哪个导航器?是的,如@meysamIzadmehr所述,您可以指定是导航器还是普通导航器(检查导航器被调用的第一个位置)可能的重复