2

I am creating react-native mobile app. I have an array with some values. I want to set array's value into input field. I have added value in the fields but i can't able to update these values. I have set my values in a qty variable like this:

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

componentWillMount() {
  var ids = [];
  this.props.data.map((dataImage,Index) => {
    dataImage['pro-name'] != undefined && (
      ids.push({'qty':dataImage['pro-qty']})    
    )
  })

  this.setState({qty:ids})
}

render() {
  return {
    this.props.data.map((dataImage,Index)=>(
      <View key={Index} style={productStyle.cartview}>
        {dataImage['pro-name'] && (
          <View style={productStyle.cartmain}>
            <Input value={this.state.qty[Index]['qty']} onChange={this.handleChangeInput.bind(this)} style={{width:40,height:40,textAlign:'left'}} />
          </View>
        )}
      </View>
    ))
  }
}

Its showing values properly into the input field but i can't able to type anything into the field to update the values. what can i do for this

2
  • You do not have handleChangeInput function defined in your code. Commented Feb 2, 2018 at 8:08
  • handleChangeInput(stateName, text) { this.setState({ [stateName]: text }) } Commented Feb 2, 2018 at 8:09

3 Answers 3

2

I will suggest you to move your input container into separate class, its better approach and each component will handle its own state. Its easy to handle and will result better in performance too.

components = []

render() {
  return this.props.data.map((item, index) => (
    <CustomComponent data={item} index={index} ref={(ref) => this.components[index] = ref} />
  ))
}

You can then get child (CustomComponent) value from its ref.

this.components[index].getValue()
this.components[index].setValue('Value');

You will need to create these functions (getValue & setValue) in CustomComponent class.

solution

Here is solution to your query. You need to install lodash or find other solution to make a new copy qty.

<Input onChangeText={(text) => this.handleChangeText(text, index)} />

handleChangeText = (text, index) => {
  const qty = _.cloneDeep(this.state.qty);
  qty[index] = {
    qty: text
  }
  this.setState({qty})
}
Sign up to request clarification or add additional context in comments.

2 Comments

I also used this but not working, actually i want to update only values that are showing in the input field. my array insert all the values in the field properly but can't updated.
This is not a complete solution, its just a sample. You need to modify it according to your needs.
0

Your Input's value is set to this.state.qty[Index]['qty']. And to change it on text edit, you can do it like this. You do not need to bind the function, instead use an arrow function like this.

onChangeText={ (newValue) => {
    this.setState({ <your-input-value-state>:newValue })
}}

Comments

0

You have to update the value of each Input individually on onChange event.

Replace your with Input with this

<Input value={this.state.qty[Index]['qty']}
       onChange={this.handleChangeInput.bind(this, Index)}
       style={{width:40,height:40,textAlign:'left'}}
/>

and update the state accordingly with the Index when the event is called

handleChangeInput(index, value){
    let {qty} = this.state;
    let qty_update = qty.slice();
    qty_update[index]['qty']  = value;
    this.setState({qty: qty_update});
}

Comments

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.