I am working on a React-Native\Redux App
- I am pre-populating a 's as they render. This works out fine. 2.After population it I would like to call a dispatch action so that the value of each is used to perform some calculations and later to be stored: I know its impossible to update state during render, but is there away I can do achieve this?
I need it to update the state as it generated the input fields.
Any help will be highly appreciated.
Thanks
handleInputChange: function(product, productId, input){
let { currentSale, updateProductQuantity } = this.props;
let updatedCurrentSale = TransactionsService.updateProductQuantity(currentSale, product, productId, input);
let transaction = TransactionsService.packageTransaction(updatedCurrentSale);
updateProductQuantity(updatedCurrentSale, transaction)
},
updateProductQuantity calls the disaptch action, and that's where we have a problem
handleQuantityValue: function(product, productId){
let { orderSelected } = this.props;
if(orderSelected){
var index = orderSelected.products.findIndex(x => x.product_id==productId)
}
var i = index
if( i >= 0){
var qtty = orderSelected.products[i].product_quantity
this.handleInputChange(product, productId, qtty).bind(this)
return qtty.toString()
}
else{
return ''
}
},
returns quantity value
renderProducts: function(){
let { currentUser, currentSale, orderSelected } = this.props;
return _.map(currentSale.productSales, function(product, productId){
var qtty = this.handleQuantityValue(product, productId)
return <View style={Styles.questionPanel}>
<Question
product={product}
productId= {productId}
Order={ orderSelected }
quantity= { qtty }
onInputChange={this.handleInputChange}
/>
</View>
}.bind(this));
},
This renders products and TextInput is populated with the quantity value. The other this is that as it renders, i should(not necessarily) dispatch an action
render: function(){
let { currentSale } = this.props;
return (
<View style={{flex: 1}}>
<NavBar navigator={this.props.navigator} />
<ScrollView>
<Text style={[Styles.dayDisplayHeader, Styles.spacing]}>
Enter Your Sale:
</Text>
{this.renderProducts()}
<Calculator
productSales={currentSale.productSales}
totalSales={currentSale.totalSales}
/>
<Button text={'Save'} whenTapped={this.handleSubmit} />
</ScrollView>
</View>
)