【问题标题】:How to pass values to other component in React-Native-Router-Flux?如何将值传递给 React-Native-Router-Flux 中的其他组件?
【发布时间】:2016-09-21 08:46:03
【问题描述】:
我的代码是:
...
<Router>
<Scene key="com1" component={Com1} initial/>
<Scene key="com2" component={Com2}/>
</Router>
...
com1.js
...
onPress={Actions.com2}
我将com1 更改为com2。
但我需要将Com1 的输入框的值传递给Com2。
我该怎么做?
【问题讨论】:
标签:
react-native
react-native-router-flux
【解决方案2】:
除此之外,(对于那些说它不起作用的 cmets 中的人)您可能想在下面尝试。当你通过
Actions.com2({text : 'Hello World'});
Com2 应该通过 'props'
const Com2 = (props) => {
return ( <View ...
{props.text}
... />
);
【解决方案3】:
通过 Input 传递数据,
import React, { Component } from 'react';
import { Text, View, TextInput, TouchableOpacity } from 'react-native';
import { Actions } from 'react-native-router-flux';
export default class Com1 extends Component {
state = { text: '' };
render() {
return (
<View>
<TextInput
value={this.state.text}
onChangeText={text => this.setState({ text })}
/>
<TouchableOpacity onPress={this.onPressNext.bind(this)}>
<Text>Get Data</Text>
</TouchableOpacity>
</View>
);
}
onPressNext() {
Actions.Com2({text: this.state.text });
}
}
在第二页获取价值
export default class Com2 extends Component {
render() {
return (
<View>
<Text>
{this.props.text}
</Text>
</View>
);
}
}
你可以参考这个链接:
https://react-native-solutions.blogspot.com/2018/07/passing-data-between-screens-in-react.html
【解决方案4】:
使用推送方式如下:
Actions.push('your key name',{prop you want to pass})
它将场景推送到堆栈,执行到新场景的过渡。
【解决方案5】:
你可以使用带有道具的动作
onPress={() => Actions.com2({title: 'some title'})}
然后在 com2 中你可以像这样检索数据
this.props.title
或者你可以使用推送属性
Actions.push('com2',{title: 'some title' } );
然后在 com2 中也这样检索它
this.props.title