【问题标题】:REACT NATIVE: How to get value of TextInputREACT NATIVE:如何获取 TextInput 的值
【发布时间】:2021-01-15 05:57:20
【问题描述】:

我想获取 TextInput Picker 的值并将其插入my Flatlist。我所做的方式我可以插入TextInput的值,但没有Picker, 或者我使用了Picker,但无法获得 TextInput 的值。 这是我的代码:

这些是使用状态

        <FlatList
          data={data}
          renderItem={({ item }) => (<Text style = {styles.item}> {item.name}</Text>)}
          keyExtractor={(item, index) => index.toString()}  
           />

【问题讨论】:

    标签: javascript reactjs react-native react-redux


    【解决方案1】:

    将数据添加到 flatlist 时忘记添加 textInput 的值。

    <Button
      title="Save"
      color="#841584"
      accessibilityLabel="Learn more about this purple button"
      onPress={() => {
        // You forgot to add the value of textInput to the data
        if (selectedValue && text) setData([...data, { name: selectedValue, textInput: text }]);
        console.log("sec");
      }}
    />
    <FlatList
      data={data}
      renderItem={({ item }) => <React.Fragment>
        <Text style={styles.item}> {item.name}</Text>
        // Displaying the data of textInput
        <Text style={styles.item}>{item.textInput}</Text>
        </React.Fragment>}
      keyExtractor={(item, index) => index.toString()}
    />
    

    【讨论】:

    • 顺便说一下如何让我的 Flatlist 可滚动,现在它不能滚动了(哈哈)
    • @Feliks_JS 默认情况下是可滚动的,您只需要为其提供更多组件来滚动并限制其高度,您可以尝试将 style={{flex:1}} 添加到平面列表。如果问题仍然存在,请开始一个新问题并删除一个链接,我也很乐意解决该问题:)
    猜你喜欢
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多