I'm using React Native and I have these functions. renderInputs() generates some inputs fields while getNextWord() does some things which are not relevant and moreover it updates the states.
getNextWord() {
// do some stuff
this.setState({ set states here });
}
renderInputs() {
var products = []
// some code that generates a randomNumber
for (let p = 0; p < randomNumber; p++){
products.push (<TextInput defaultValue="" maxLength={1} key={p} onChangeText={(text) => this.handleChange(text, p)}></TextInput> );
}
return products
}
In my render() method I show the input fields and I placed I button that everytime is clicked it triggers getNextWord() function.
<View>{this.renderInputs()}</View>
<Button onPress={this.getNextWord.bind(this)}>Get Next Word</Button>
Now, I can correctly see the input fields, and everytime I click on "Get Next Word" button renderInputs() generates a new bunch of input fields.
My problem is that if I type a character inside one of these input field once I click button I still see that letter, while I'd like to have all my input fields empty at the beginning.
I tried using defaultValue="" but it seems it doesn't work.
If I do: defaultValue="A" this is what I get once I run the app.
example:
First time renderInputs() is called:
_ _ _ _ _ _ // empty inputs (let's say randomNumber is 6)
then I type in the second input field a letter, let's say "G"
_ G _ _ _ _
once I click "Get Next Word" button renderInputs() generates randomNumber (let's say generated randomNumber is 8) input fields and this is what I get:
A G A A A A A A
but I want to have:
A A A A A A A A