您好我正在嘗試在我的項目中執行switch語句。我有一個對象圖像如下export const images = [ { image: BASE.URL + 'Images/Plumber.png', }, { image: BASE.URL + 'Images/electrician.png', }, { image: BASE.URL + 'Images/ac.png', }]我正在從服務器獲取工作人員列表并將其呈現在Card.so中,因此服務器響應僅包含工作人員的名稱。我正在嘗試將圖像與它們一起提供,因此我編寫了switch語句,但圖像未與文字一起。以下是我的代碼。 import { images } from './data'; renderImage() { const { workType } = this.state; switch (workType) { case 'Plumber': return ( <Image style={{ height: 100, width: 100 }} source={{ uri: images[0].image }} /> ); case 'Electrician': return ( <Image style={{ height: 100, width: 100 }} source={{ uri: images[1].image }} /> ); case 'AC' return ( <Image style={{ height: 100, width: 100 }} source={{ uri: images[2].image }} /> ); } } render(){ const { workers,workType } = this.state; return( {workers.map((a, index) => ( <TouchableOpacity onPress={() => this.popUpDialog(a.id, a.work_type)}> <Card> {this.renderImage()} <Text>{a.work_type}</Text> </Card> </TouchableOpacity> ))} ) }我做錯了什么,請幫助我找到解決方案。謝謝!
如何在React Native中使用switch執行條件渲染?
Helenr
2021-03-29 15:31:05