【问题标题】:Set state is only working after clicking twice react native设置状态仅在单击两次后才起作用本机反应
【发布时间】:2022-01-06 21:32:40
【问题描述】:

我试图在用户单击按钮时更改按钮的不透明度,(在 2 个按钮之间,较暗的一个将是他的选择)。出于某种原因,每当我单击按钮时,不透明度不会改变,并且仅在单击 2 次后才会改变。任何想法我哪里出错了?

export default function lastName({ navigation }) {
  let [opac, setOpacity] = useState(0.3);

  return (
    <View style={styles.container}>
      <View
        style={{
          flex: 0.5,
          justifyContent: "flex-end",
          //backgroundColor: "red",
        }}
      >
        <Text style={styles.textStyle}>
          
         Select a button
        </Text>
      </View>
      <View
        style={{
          flex: 1,
          justifyContent: "flex-start",
        }}
      >
        <TouchableOpacity

          style={{
            marginTop: 70,
            alignItems: "center",
            justifyContent: "center",
            width: 180,
            height: 40,
            backgroundColor: "#004953",
            opacity: opac,/// here 
            borderRadius: 100,
          }}
          onPress={() => {
            setOpacity(1); // here
          }}
        >
          <Text
            style={{
              color: "white",
              textAlign: "center",
              fontSize: 18,
            }}
          >
            Button 1
          </Text>
        </TouchableOpacity>


【问题讨论】:

    标签: react-native touchableopacity


    【解决方案1】:

    显然,这是一个可触摸不透明度的错误,您必须将不透明度包装在视图中,请参阅此链接Dynamic Opacity not changing when component rerenders in react native

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2022-12-16
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    相关资源
    最近更新 更多