Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+7 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 (2.4k points)

2 Answers

+2 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 (2.4k points)
+2 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 (1.4k points)
...