由于某些原因,当我第二次调用我的组件TouchSelect时,它没有显示,所以我不能重用它.我已经重用了组件 很多次了,所以我真的对此感到困惑. 这个问题似乎与FlatList有关,因为如果我做同样的事情,但使用map函数,一切都会完美地工作,我可以重用我的组件而不会有任何问题.

我还注意到,如果我在TouchSelect组件下面插入一个文本,我看不到它;如果我把文本放在组件前面,我会毫不犹豫地看到它. 这里是存储库:https://github.com/Themrpie/surveyapp

export default function App() {

  

  return (
    <SafeAreaView style={styles.container}>
      <RadioSelect />
      <ChipSelect/>
      <Stars/>      
      <TouchSelect question='How likely are you to recommend our company?' />
      <TouchSelect question='THIS DOESNT SHOW' />
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    
    
  },
  texto: {
    justifyContent:'center'
  }
});

以防我也发布完整的代码组件:

import { FlatList, SafeAreaView, StyleSheet, Text, TouchableOpacity, View } from 'react-native'
import React, { useState } from 'react'
import { MD2Colors, Surface } from 'react-native-paper'

const options = [
    {
        id: 1,
        option: 1
    },
    {
        id: 2,
        option: 2
    },
    {
        id: 3,
        option: 3
    },
    {
        id: 4,
        option: 4
    },
    {
        id: 5,
        option: 5
    },
    {
        id: 6,
        option: 6
    },
    {
        id: 7,
        option: 7
    }, 
    {
        id: 8,
        option: 8
    },
    {
        id: 9,
        option: 9
    },
    {
        id: 10,
        option: 10
    },
    
   
]

const TouchSelect = ({question}) => {
    const [selected, setSelected] = useState()

  return (
      <Surface style={styles.container}>    
        <Text style={styles.question} >{question}</Text>      
         <FlatList data={options} keyExtractor={(item) => item.id} horizontal
            renderItem={({item}) =>(
                <TouchableOpacity style={ selected === item.id? [{...styles.option}, {backgroundColor:MD2Colors.lightGreen600}]: styles.option} 
                    onPress={() => setSelected(item.id)}  >
                    <Text style={styles.texto} >{item.option}</Text>
                </TouchableOpacity> )
            }
            />       
     </Surface>
  )
}

export default TouchSelect

const styles = StyleSheet.create({
    container: {
        marginVertical: 20,
        backgroundColor:'white',
        alignItems:'center'

    },
    option: {
        marginHorizontal:2,
        backgroundColor: MD2Colors.amberA200,
        padding: 8,        
        borderRadius: 100,        
        
    },
    question: {
        fontSize: 20,
        fontWeight: 'bold',
        marginBottom: 15,
        textAlign: 'center',
        
    },
    texto: {
        fontSize: 20,
        fontWeight: 'bold',
        textAlign:'center',
        
        
        
    }

})

如果有人能帮我解决这个问题,我将不胜感激.

推荐答案

把你的App分装在ScrollView里.

import { ScrollView } from 'react-native';  
export default function App() {

  return (
    <SafeAreaView style={styles.container}>
      <ScrollView> // <---- Try to add this
        <RadioSelect />
        <ChipSelect/>
        <Stars/>      
        <TouchSelect question='How likely are you to recommend our company?' />
        <TouchSelect key={2} question='THIS DOESNT SHOW' />
      </ScrollView>
    </SafeAreaView>
  );
}

Reactjs相关问答推荐

尽管在CLI中收到来自Stripe的OK Post请求,但没有webhook Post请求的处理- NextJS 14

为什么我的标签在Redux API中不能正常工作?

react 路由导航不工作,但手动路由工作

在Reaction+Redux+RTKQuery中对API调用进行排序

如何使数据库数据在第一次呈现时显示?

在每页上应用字体-NextJS

阻止组件更新的多分派Redux Thunk或setState是否有任何问题

ReactJS:唯一项目的数量总和

在React中使用映射创建flex组件

Github 操作失败:发布 NPM 包

PWA:注册事件侦听器不会被触发

单击按钮不会切换组件(当按钮和组件位于两个单独的文件中时)

解决在React测试库中的act()警告

React对象值下降2次而不是1次

当我在 React Router Dom 中使用两个参数并直接在页面上导航时,数据获取没有发生

在 Formik 表单中访问子组件的值

从 API 中提取映射数组后出现重复元素

将 Material UI 菜单渲染为

如何在 react-router-dom v6 中实现监听功能?

仅在重新渲染后设置状态