我的代码是:
...
<Router>
<Scene key="com1" component={Com1} initial/>
<Scene key="com2" component={Com2}/>
</Router>
...
com1.js
...
onPress={Actions.com2}
我com1
改为com2
.
Com1
但我需要为to的输入框传递值Com2
。
我怎样才能做到这一点?
我的代码是:
...
<Router>
<Scene key="com1" component={Com1} initial/>
<Scene key="com2" component={Com2}/>
</Router>
...
com1.js
...
onPress={Actions.com2}
我com1
改为com2
.
Com1
但我需要为to的输入框传递值Com2
。
我怎样才能做到这一点?
您可以像这样传递数据:
Actions.com2 ({text: 'Hello World'})
您可以像这样在 com2 中恢复数据:
this.props.text
您可以转到下一个教程以获取更多信息:
https://github.com/aksonov/react-native-router-flux/blob/master/docs/v3/MINI_TUTORIAL.md
除此之外,(对于那些在评论中说它不起作用的人)您可能想在下面尝试。当你通过
Actions.com2({text : 'Hello World'});
Com2 应该通过“道具”
const Com2 = (props) => {
return ( <View ...
{props.text}
... />
);
通过 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
使用推送方法如下:
Actions.push('your key name',{prop you want to pass})
它将场景推送到堆栈,执行到新场景的转换。
你可以使用带有道具的动作
onPress={() => Actions.com2({title: 'some title'})}
然后在 com2 中,您可以像这样检索数据
this.props.title
或者你可以使用 push 属性
Actions.push('com2',{title: 'some title' } );
然后像这样在 com2 中检索它
this.props.title