简体   繁体   English

如何更改 Flatlist 中的 textInput 值 react native?

[英]How to change textInput value in Flatlist react native?

I am working with a list of products I need to modify the quantities but I can not modify the inputText that contains the quantity with the (+) and (-) buttons我正在处理需要修改数量的产品列表,但我无法使用 (+) 和 (-) 按钮修改包含数量的 inputText

I appreciate any help..thanks我感谢任何帮助..谢谢

在此处输入图像描述

I can not work with stados because you only have to change one row at a time我不能使用 stados,因为你一次只需要更改一行

I would like to know a good way so that when I press the buttons the number will be modified in each of the lines try with states but I change the content of all the inputs我想知道一个好方法,这样当我按下按钮时,每行中的数字都会被修改 try with states 但我更改了所有输入的内容

code:代码:

 <View style={styles.containerPro}> <List containerStyle={{borderTopWidth: 0, borderBottomWidth: 0}} key={this.state.coins.length} dataSource={this.state.number}> <FlatList extraData={this.state} data={this.state.coins} keyExtractor={(item) => item.id} renderItem={({item}) => { let num = 0; this.state={ numb:num, }; let url = 'http://laeconomiadelsur.com/upload/' + item.foto; const tableData = [ [<View> <Image source={{uri: url}} style={{ width: 50, height: 70, }}/> </View>, ], [<View style={styles.cellIcons}> <Text style={styles.textCells}> {item.nombre}</Text> <Text style={styles.textCellsDescription}> {item.descripcion}</Text> <Text style= {styles.textCellsPrecio}>${item.precio.toLocaleString()} </Text> </View>, ], [<View> <Text style={styles.cantidad1}>subtotal: <Text>$300000</Text></Text> <View style={styles.buttonsAdd}> <TouchableOpacity style={styles.button1} navigation={this.props.navigation} onPress={()=>{ num = num-1; console.log(num) }} > <Text>-</Text> </TouchableOpacity> <TextInput style={styles.textSum} value={this.state.numb.toString()} placeholder="0" underlineColorAndroid={"transparent"} /> <TouchableOpacity style={styles.button2} navigation={this.props.navigation} onPress={(num) => { num = num +1; console.log(num) } } > <Text>+</Text> </TouchableOpacity> </View> <Text style={styles.cantidad}>cantidad</Text> </View>,] ]; return ( <View style={{flex: 1}} key={item.id}> <Table style={styles.table} borderStyle={{borderWidth: 2, borderColor: 'white'}} key={item.id}> <Cols data={tableData} flexArr={[1, 2, 2]} style={styles.cells} key={item.id}/> </Table> </View> ); }} /> </List> </View>

Create a separate component for tableData.为 tableData 创建一个单独的组件。 And using并使用

shouldComponentUpdate() 

inside the tableData component you render only component you want.在 tableData 组件中,您只呈现所需的组件。 There is a good reading about life cycle https://reactjs.org/docs/react-component.html and about关于生命周期https://reactjs.org/docs/react-component.html和关于

shouldComponentUpdate() 

you can read from here你可以从这里阅读

https://reactjs.org/docs/react-component.html#shouldcomponentupdate https://reactjs.org/docs/react-component.html#shouldcomponentupdate

what eh solved using: react-native-numeric-input使用以下方法解决了什么问题: react-native-numeric-input

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM