admin管理员组文章数量:1397228
I am wondering how I can get data from ponents when button is clicked in React Native and pass it to other ponent. Here is my code:
This is my input ponent:
ZipInput.js
import React from 'react';
import { TextInput } from 'react-native';
import styles from './../assets/style';
export default class ZipInput extends React.Component {
constructor(props) {
super(props);
this.state = '';
}
render() {
return (
<TextInput
style={styles.input}
onChangeText={(text) => this.setState({text})}
keyboardType={'numeric'}
placeholder = {'Enter Zip Code'}
/>
);
}
}
This is my button:
GoButton.js
import React from 'react';
import { Button, View } from 'react-native';
import styles from './../assets/style';
import {StackNavigator} from 'react-navigation';
export default class GoButton extends React.Component {
_handlePress(event) {
alert('Pressed!');
}
render() {
const {navigate} = this.props.navigateProp
return (
<View style={styles.buttonContainer}>
<Button
onPress={() =>
navigate('ZipResultScreen')
}
title="GO"
accessibilityLabel="Find Insurance Quotes"
color='#fff'
/>
</View>
);
}
}
I created the ponents serparetely and import them in Homescreen.js. From there I will pass the data to other ponent.
Here is my Homescreen.js:
import React from 'react';
import { StyleSheet, Text, View, ImageBackground } from 'react-native';
import styles from '../assets/style'
import Header from '../ponents/header';
import ZipInput from '../ponents/zipinput';
import InsuranceType from '../ponents/insurancetype';
import GoButton from '../ponents/gobutton';
import {StackNavigator} from 'react-navigation';
export default class HomeScreen extends React.Component {
render() {
const navigate = this.props.navigation;
return (
<View style={styles.container}>
<Header />
<ImageBackground
style={styles.imgbg}
source={{ uri: '.jpg' }}
>
<View style={styles.intro}>
<Text style={styles.title}>Compare Insurance Rates</Text>
<ZipInput />
<InsuranceType style={styles.select} />
<GoButton navigateProp = {navigate} />
</View>
</ImageBackground>
</View>
);
}
}
I am wondering how I can get data from ponents when button is clicked in React Native and pass it to other ponent. Here is my code:
This is my input ponent:
ZipInput.js
import React from 'react';
import { TextInput } from 'react-native';
import styles from './../assets/style';
export default class ZipInput extends React.Component {
constructor(props) {
super(props);
this.state = '';
}
render() {
return (
<TextInput
style={styles.input}
onChangeText={(text) => this.setState({text})}
keyboardType={'numeric'}
placeholder = {'Enter Zip Code'}
/>
);
}
}
This is my button:
GoButton.js
import React from 'react';
import { Button, View } from 'react-native';
import styles from './../assets/style';
import {StackNavigator} from 'react-navigation';
export default class GoButton extends React.Component {
_handlePress(event) {
alert('Pressed!');
}
render() {
const {navigate} = this.props.navigateProp
return (
<View style={styles.buttonContainer}>
<Button
onPress={() =>
navigate('ZipResultScreen')
}
title="GO"
accessibilityLabel="Find Insurance Quotes"
color='#fff'
/>
</View>
);
}
}
I created the ponents serparetely and import them in Homescreen.js. From there I will pass the data to other ponent.
Here is my Homescreen.js:
import React from 'react';
import { StyleSheet, Text, View, ImageBackground } from 'react-native';
import styles from '../assets/style'
import Header from '../ponents/header';
import ZipInput from '../ponents/zipinput';
import InsuranceType from '../ponents/insurancetype';
import GoButton from '../ponents/gobutton';
import {StackNavigator} from 'react-navigation';
export default class HomeScreen extends React.Component {
render() {
const navigate = this.props.navigation;
return (
<View style={styles.container}>
<Header />
<ImageBackground
style={styles.imgbg}
source={{ uri: 'https://www.expertinsurancereviews./wp-content/uploads/2017/03/background-hp-1.jpg' }}
>
<View style={styles.intro}>
<Text style={styles.title}>Compare Insurance Rates</Text>
<ZipInput />
<InsuranceType style={styles.select} />
<GoButton navigateProp = {navigate} />
</View>
</ImageBackground>
</View>
);
}
}
Share
Improve this question
asked Jan 29, 2018 at 15:51
Ilkin GuluzadaIlkin Guluzada
1332 gold badges3 silver badges11 bronze badges
1
- possible duplicate of stackoverflow./questions/37949187/… – Nir Ben-Yair Commented Jan 29, 2018 at 16:00
2 Answers
Reset to default 1You need to pass your data while navigating to another screen.
Consider following example.
<Button onPress={() => navigation.navigate('ScreenName', { data: { title: 'Hello World'} })}>
export default class Screen extends React.Component {
render () {
const { title } = this.props.navigation.state.params.data
return (
<View>
<Text>{title}</Text>
</View>
)
}
}
Here is what I found from React Navigation Documentation. It is so easy.
https://reactnavigation/docs/params.html
本文标签: javascriptHow to pass data between components in React NativeStack Overflow
版权声明:本文标题:javascript - How to pass data between components in React Native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744107722a2591142.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论