简体   繁体   中英

React Native: how to send value of TextInput in React Native?

I am trying to send value of TextInput to another screen, can someone Help me with please.. Here is my code:

  const [text, setText] = useState ( ' ');
  return (
<>
        <TextInput style={styles.input}
          underlineColorAndroid="transparent"
          placeholder="   Type "
          placeholderTextColor="#9a73ef"
          autoCapitalize="none"
          onChangeText={text => setText(text)}
          //defaultValue={text}
        />
 <Button
        title="Go to Details"
        onPress={() => navigation.navigate('Details')}
      />
-----

this is my Detais Screen:

function DetailsScreen(  {text}) {
  return (

<View>


  <Text> Hello this is Details  Screen + { text }</Text>
</View>
   
  );
}

can't take any value, even made a prop

You should pass the parameter like below

 <Button
        title="Go to Details"
        onPress={() => navigation.navigate('Details',{text:text})}
      />

And in your details screen you should receive it like below

function DetailsScreen(  {route}) {
  return (
<View>
  <Text> Hello this is Details  Screen + { route.params.text }</Text>
</View>
  );
}

Refer the official docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM