3

How we can access values from dynamically created Text Input. Like flat list creates 3 text inputs and then on click of button we validate which one is added and which one not. How to manage multiple state array. currently I done this

const data = [1,2];

constructor(props) {
super(props);
this.state = {
  Textdata:[],
};

}

  SubmitButton(){
//how to access those text input values and add to array
}


<FlatList
data={data}
renderItem={this.renderItem}
keyExtractor={(item, index) => item}
 />

renderItem = ({ item, index }) => {

return (
    <View>
     <Item 
        floatingLabel 
        style={styles.InputBoxStyle}
        >
        <Label>First Name</Label>
        <Input 

          />
     </Item>
     <Item 
        floatingLabel 
         style={styles.InputBoxStyle}>
         <Label>Last Name</Label>
        <Input 

          />
      </Item>
<Button rounded
        onPress={this.SubmitButton}>
        <Text>Submit</Text>
     </Button>

    </View>
 );
};

1 Answer 1

1

You can store the text of inputs in your state. Just listen for onChangeText event. Try to add this to your input :

onChangeText={val => updateState(index, val) />

Where index is your item's index from renderItem function. Later add method :

updateState = (index,value) => {
   const Textdata = [...this.state.Textdata]; //make a copy of array
   Textdata[index] = value;
   this.setState({ Textdata: Textdata });
}

Then when pressing the button you can verify this array.

As another example, when value from Picker component changes you can handle it in this way:

onValueChange={itemValue => this.setState({someNameForThisPicker: itemValue})}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you this solution is working. How to handle picker(dropdown) & its selected state array.
Basically the same way - on some event (onChange, onPress, etc.) attach a function that will update your state. Don't forget to make a copy of the data, you're going to update.
I have tried onValueChange method by providing function to it change the state of the selected picker, Please Can you provide the sample example
I have tried given solution but still its not working. will you help where am I going wrong? link
86 line : newArray[value] = index; -> change that to newArray[index] = value; And : swap arguments in onValueChange function -> onValueChange(index: number, value: string)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.