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

标签 javascript react-native

我正在处理一个产品列表,我需要修改数量,但无法使用 (+) 和 (-) 按钮修改包含数量的 inputText

感谢任何帮助..谢谢

enter image description here

我无法使用 stados,因为你一次只需更改一行

我想知道一种好方法,这样当我按下按钮时,每一行中的数字都会被修改,尝试使用状态,但我更改所有输入的内容

代码:

<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>

最佳答案

为 tableData 创建一个单独的组件。并使用

shouldComponentUpdate() 

在 tableData 组件内,您仅渲染所需的组件。 关于生命周期有一本很好的读物https://reactjs.org/docs/react-component.html以及关于

shouldComponentUpdate() 

您可以从这里阅读

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

关于javascript - 如何更改 Flatlist react native 中的 textInput 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52047534/

相关文章:

javascript - 选择的 jquery 插件的输入限制

javascript - 在不填满磁盘的情况下检测 Firefox IndexedDB 或 Web Storage 存储限制?

react-native - 如何在 React Native 中移动下一个 textInput

React-Native WebView 打开 Google Docs 移除 header

React-native 占位符在 Android 上仅部分可见

react-native - GraphQL 服务器可以返回图像吗?

由于 RegExp 语法,JavaScript block 注释过早结束

javascript - 从 Controller 观察工厂变量

javascript - 查找更改 &lt;input/> 标记的值属性的脚本

reactjs - 使用 ScrollView 避免键盘输入