Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+11 votes

I am using react native, but I am pretty sure it translates 1:1 to react. So here is a code snippet:

<Input
        name="CommonName"
        value = {this.state.name}
        onChangeText= {this.setState({commonName: value})}>
</Input>

So when I have this code in my render method I receive an error. 'Cannot find variable: value'. I want to grab the value of the input and change my react state with the value.

asked in CSC490_Spring202021 by (8 points)

2 Answers

+3 votes
 
Best answer

I needed to pass a variable into the function so:

onChangeText= {value=>this.setState({commonName: value})}

I need to use setState to change the value of the state, and I need to change this react's state. The challenge was that I did not know how value was in context to the react state, so I had to pass it into the function.

answered by (8 points)
+3 votes

I am not sure this helps or not but "value" in this situation is a part of input, not a const or value that you can assign to setState. Therefore, set another var outside for input value. Then use setState for that value.

answered by (8 points)
...