应用程序崩溃是因为平面列表API调用

发布于 2025-01-24 12:43:15 字数 3090 浏览 2 评论 0原文

我正在调用API并在日志中看到相应的API值时,它显示了正确的值,但是当我尝试借助Hooks的应用程序崩溃,尝试将API设置在平面列表中。我不知道我是新手React Native的原因,因此对此的任何帮助将不胜感激。 注意(如果我直接显示没有平面列表的值,则不会导致任何错误)

function Item({ item }) {
    const navigation = useNavigation();   
  
      return (
        

        <TouchableOpacity style={styles.listItemBox}
        onPress={() => navigation.navigate('PatientDemographics')}
        >
          <View style={{flex:1}}>

            <Text numberOfLines={1} style={{ textAlign: 'left', fontSize: 25, color:"#075430", textAlign: 'center',fontFamily:"Montserrat-Regular"}}>{item.firstName}</Text>

        
            <TouchableOpacity style={[styles.smallRoundedBlueRoundedNoMargin,{marginTop:10,marginBottom:40}]}
                   onPress={() => navigation.navigate('PatientDemographics')} >
              <Text style={[styles.cardText,{fontSize: 18},{color: 'white'}]}>SELECT </Text>
            </TouchableOpacity>
      


              </View>


      
 
        </TouchableOpacity>
       
            );
    }

const SelectPatient = () => {

   
   let  numColumns = 4;
    const formatData = (data, numColumns) => {
     const numberOfFullRows = Math.floor(data.length / numColumns);
 
       let numberOfElementsLastRow = 8 - (numberOfFullRows * numColumns);
       while (numberOfElementsLastRow !== numColumns && numberOfElementsLastRow !== 0) {
         data.push({ key: `blank-${numberOfElementsLastRow}`, empty: true });
         numberOfElementsLastRow++;
        
       }
       return data;
     };
      
  // const navigation = useNavigation(); 

     const [isLoading, setLoading] = useState(true);
     const [patient, setPatient] = useState([]);
     const mrnum=89
  
     useEffect(() => {
     
      axios({
          method: 'get',
          url: `https://emr-system.000webhostapp.com/emrappointment/emrappointment/patient/search?mrnum=89&cnic=&qrcode=`,
        }).then((response) => {
  
              //Balance / transaction-list
              setPatient(response.data.result);
              console.log(response.data.result);
              console.log(patient[0].patientId);
  
        }).then(() => setLoading(false));
    }, []);
  
   
  
 

    
    return (
    
        <View style={styles.container}>
          <Header name="Select Patient" class= ""/>
          
      
            <UnitClerkHeader/>
            <PatientHeader/>
         <View style= {{flex:1 ,width: '100%', alignSelf: 'center'}}>
         <SafeAreaView style={{flex:1}} >
        <FlatList
  
          style={{flex:1, marginTop: 30, marginRight:30,marginLeft:30}}
          data={ formatData(patient,numColumns)}
          renderItem={({ item }) => <Item item={item}/>}
          keyExtractor={item => item.patientId}
          numColumns = {numColumns}
         
        /> 
        </SafeAreaView>
  

        </View>

        </View>
       
      
    
    );
  }
export default SelectPatient;

I'm calling API and seeing the respective API values in the log, it shows me correct values, but when I try to set API in Flat list with the help of hooks my app crashes. I don't know the reason as I'm new in react native, so any help regarding this would be really appreciated.
NOTE( If I'm displaying the values directly without flat list it won't cause any error)

function Item({ item }) {
    const navigation = useNavigation();   
  
      return (
        

        <TouchableOpacity style={styles.listItemBox}
        onPress={() => navigation.navigate('PatientDemographics')}
        >
          <View style={{flex:1}}>

            <Text numberOfLines={1} style={{ textAlign: 'left', fontSize: 25, color:"#075430", textAlign: 'center',fontFamily:"Montserrat-Regular"}}>{item.firstName}</Text>

        
            <TouchableOpacity style={[styles.smallRoundedBlueRoundedNoMargin,{marginTop:10,marginBottom:40}]}
                   onPress={() => navigation.navigate('PatientDemographics')} >
              <Text style={[styles.cardText,{fontSize: 18},{color: 'white'}]}>SELECT </Text>
            </TouchableOpacity>
      


              </View>


      
 
        </TouchableOpacity>
       
            );
    }

const SelectPatient = () => {

   
   let  numColumns = 4;
    const formatData = (data, numColumns) => {
     const numberOfFullRows = Math.floor(data.length / numColumns);
 
       let numberOfElementsLastRow = 8 - (numberOfFullRows * numColumns);
       while (numberOfElementsLastRow !== numColumns && numberOfElementsLastRow !== 0) {
         data.push({ key: `blank-${numberOfElementsLastRow}`, empty: true });
         numberOfElementsLastRow++;
        
       }
       return data;
     };
      
  // const navigation = useNavigation(); 

     const [isLoading, setLoading] = useState(true);
     const [patient, setPatient] = useState([]);
     const mrnum=89
  
     useEffect(() => {
     
      axios({
          method: 'get',
          url: `https://emr-system.000webhostapp.com/emrappointment/emrappointment/patient/search?mrnum=89&cnic=&qrcode=`,
        }).then((response) => {
  
              //Balance / transaction-list
              setPatient(response.data.result);
              console.log(response.data.result);
              console.log(patient[0].patientId);
  
        }).then(() => setLoading(false));
    }, []);
  
   
  
 

    
    return (
    
        <View style={styles.container}>
          <Header name="Select Patient" class= ""/>
          
      
            <UnitClerkHeader/>
            <PatientHeader/>
         <View style= {{flex:1 ,width: '100%', alignSelf: 'center'}}>
         <SafeAreaView style={{flex:1}} >
        <FlatList
  
          style={{flex:1, marginTop: 30, marginRight:30,marginLeft:30}}
          data={ formatData(patient,numColumns)}
          renderItem={({ item }) => <Item item={item}/>}
          keyExtractor={item => item.patientId}
          numColumns = {numColumns}
         
        /> 
        </SafeAreaView>
  

        </View>

        </View>
       
      
    
    );
  }
export default SelectPatient;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

完美的未来在梦里 2025-01-31 12:43:15

您可以尝试我可以更好地帮助您。

<FlatList
  style={{ flex: 1, marginTop: 30, marginRight: 30, marginLeft: 30 }}
  data={() => formatData(patient, numColumns)}
  renderItem={({ item }) => <Item item={item} />}
  keyExtractor={item => item.patientId}
  numColumns={numColumns}
/>

当您显示错误时,

You can try with

<FlatList
  style={{ flex: 1, marginTop: 30, marginRight: 30, marginLeft: 30 }}
  data={() => formatData(patient, numColumns)}
  renderItem={({ item }) => <Item item={item} />}
  keyExtractor={item => item.patientId}
  numColumns={numColumns}
/>

I can help you better when you show your error too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文