首页 > 解决方案 > 渲染多个内部地图反应原生

问题描述

我无法在地图内渲染多个开关。

我有一个看起来像这样的数组:

array: [
   {
      "title": "test1"
   },
   {
      "title": "test2"
   }
]

然后在渲染和返回中,我使用地图并希望显示 2 个开关:

{this.state.array.map((item, index) => (
   <View key={index}>
     <Switch
       trackColor={{ false: "#767577", true: "#81b0ff" }}
       thumbColor={isEnabled ? "#f5dd4b" : "#f4f3f4"}
       ios_backgroundColor="#3e3e3e"
       onValueChange={toggleSwitch}
       value={isEnabled}
      />             
    </View>
))}

每个开关的“isEnable”都必须是唯一的,我不知道这样做的最佳实践?

标签: reactjsreact-native

解决方案


array: [
   {
      "title": "test1",
      isEnabled:false,
   },
   {
      "title": "test2"
      isEnabled:true,
   }
]


{this.state.array.map((item, index) => (
   <View key={index}>
     <Switch
       trackColor={{ false: "#767577", true: "#81b0ff" }}
       thumbColor={item.isEnabled ? "#f5dd4b" : "#f4f3f4"}
       ios_backgroundColor="#3e3e3e"
       onValueChange={toggleSwitch}
       value={item.isEnabled}
      />             
    </View>
))}

推荐阅读