简体   繁体   English

React 本机开关不可点击

[英]React native switch is not clickable

here is the code这是代码

const toggleSwitch = () => {
  console.log("clicked");
  props.updateAvailability(!isEnabled);
  setIsEnabled(!isEnabled);
};

Render part渲染部分

<View
  style={{
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "space-between",
  }}
>
  <Text style={styles.name}>{props.name}</Text>
  <View style={{ flex: 1 }}>
    <Switch
      trackColor={{ false: "#767577", true: Colors.borderColor }}
      thumbColor={isEnabled ? Colors.green : "#f4f3f4"}
      ios_backgroundColor="#3e3e3e"
      onValueChange={toggleSwitch}
      value={isEnabled}
    />
  </View>
</View>;

style part样式部分

name: {
  fontSize: 18,
  fontWeight: "700",
  color: Colors.black1,
  marginRight: 40,
},

if the props.name have bigger length then the switch is not working如果props.name的长度较大,则开关不起作用

示例图像

in the picture last switch is not working because the length of the props.name图片中最后一个开关不起作用,因为props.name的长度

You can use width instead of flex and it will work.您可以使用宽度而不是 flex,它会起作用。

<View
style={{
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "space-between",
    width:"100%"// add this
  }}
>
  <Text style={styles.name}>{props.name}</Text>
  <View style={{ width:"30%" // add this and remove flex:1 }}>
    <Switch
      trackColor={{ false: "#767577", true: Colors.borderColor }}
      thumbColor={isEnabled ? Colors.green : "#f4f3f4"}
      ios_backgroundColor="#3e3e3e"
      onValueChange={toggleSwitch}
      value={isEnabled}
    />
  </View>
</View>;


name: {
 fontSize: 18,
  fontWeight: "700",
  color: Colors.black1,
  marginRight: 40,
  width:"60%"// add this
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM