0

I am working on a very simple reminders app as an introduction to using react and javascript. (i am a complete beginner at javascript, but have experience with languages like ruby.) I want to be able to get a user keyboard input from this TextInput component (https://docs.expo.io/versions/v37.0.0/react-native/textinput/#content) without using a class. I just want to store the input as a variable and be able to call it up anywhere else in my code. The {text} variable in the second function is where i want to store the input from the first function. (apologies for bad english) i try to run it, and it says that it couldnt find variable 'text', even though thats what its being assigned to.

function NewReminderScreen({ navigation }) {
  const [value, onChangeText] = React.useState('click to add reminder.')

  return (
    <View style={styles.container}>
      <TextInput
        style={{height: 40, borderColor: 'white', borderWidth: 0, color:'#595959', fontSize:20, marginHorizontal:5}}
        onChangeText={text => onChangeText(text)}
        value = {value}
      />   
);


}

function HomeScreen({ navigation }) {

  return (
    <View style={styles.container}>
      <Text style={styles.buttontext}>{text}</Text>

      <TouchableOpacity
        onPress={() => navigation.navigate('NewReminderScreen')}
        style={styles.plusbutton}>
        <Text style={styles.buttontext}>+</Text>

      </TouchableOpacity>
    </View>
  );
}
snalrus
  • 5
  • 2
  • 5

2 Answers2

0

Can you try by declaring a variable outside the function and assigning the value to it when the function inside onChange prop is fired?

  let text = '';

  function NewReminderScreen({ navigation }) {
  const [value, onChangeText] = React.useState('click to add reminder.')

  function textChangeHandler(event){
    onChangeText(event.target.value)
     text = event.target.value;
    }

  return (
    <View style={styles.container}>
      <TextInput
        style={{height: 40, borderColor: 'white', borderWidth: 0, color:'#595959', fontSize:20, marginHorizontal:5}}
        onChange={textChangeHandler}
        value = {value}
      />   
);


}
Ajin Kabeer
  • 2,096
  • 2
  • 9
  • 18
0

SCREEN 1 You can make a constructor like this

function NewReminderScreen({ navigation }) {
     constructor(props){
          super(props)
          this.state = {
          text: ''

          }
       }
  const [value, onChangeText] = React.useState('click to add reminder.')



 return (
    <View style={styles.container}>
      <TextInput
        style={{height: 40, borderColor: 'white', borderWidth: 0, color:'#595959', fontSize:20, marginHorizontal:5}}
        onChangeText={text => this.setState({text})}
        value = {value}
      />   
);


}

Put this into your button click function to navigate to your HomeScreen

   this.props.navigation.replace('HomeScreen', { 
     text: this.state.text
  })

SCREEN 2

And put this after render in your homescreen

    const text = this.props.route.params.text;

And put this anywhere you want in your contain

 <Text style={styles.buttontext}>{text}</Text>
learner
  • 214
  • 1
  • 3
  • 10