如何在 React Native 中将 TextInput 值从一个屏幕传递到另一个屏幕? [英] How to pass TextInput value from one screen to another in react native?
问题描述
我知道这个问题已经有人问过了,但我没有找到任何合适的解决方案.所以这是我的问题,我想在提交按钮上将 TextInput 值传递到另一个屏幕.所以,请告诉我如何传递值并在另一个屏幕上显示.我是 React Native 开发的新手.
I know this question is already asked but i didn't find any proper solution. So here is my question, i want to pass TextInput value to another screen on submit button.So, please tell me How to pass the value and display on another screen. I am new in react native development.
推荐答案
在组件的顶部,您要声明您的状态.
At the top of your component you want to declare your state.
class App extends Component {
state = {text: ""};
然后您想将 TextInput 中的任何文本保存到该状态.
Then you want to save whatever text is in your TextInput to that state.
<TextInput onChangeText={text => this.setState({text})} />
然后你想把那个状态传递给你需要它的另一个组件.
Then you would want to pass that state to the other component you would need it in.
将此添加到提交按钮的 onPress:
Add this to the onPress of your submit button:
onPress={() => navigate('OtherComponent', { text: this.state.text} )}
然后在您的其他组件"中,您可以像这样访问状态:
Then in your "OtherComponent" you can access the state like this:
this.props.navigation.state.params.text
这里假设您使用的是 react-navigation.
This is assuming that you are using react-navigation.
这篇关于如何在 React Native 中将 TextInput 值从一个屏幕传递到另一个屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!