为什么我需要在TouchableOpacity上单击两次?

发布于 2025-01-30 06:32:44 字数 1064 浏览 5 评论 0原文

我想创建一个按钮,如果您单击它,则复制所述按钮的颜色。 但是我需要单击两次才能复制颜色(我第一次什么都不复制) 有人可以帮我还是告诉我在哪里犯错? 这是我的代码:

  const [couleur, setCouleur] = useState("");
  async function cop() {
    await navigator.clipboard.writeText(couleur);
    alert("Couleur copiée");
  }

           <TouchableOpacity
                onPress={() => {
                  setCouleur("black");
                  cop();
                }}
              >
                <View
                  style={{
                    width: "100%",
                    height: 30,
                    backgroundColor: "#ADDAD4",
                  }}
                >
                  <Text style={styles.textcol}>green-8</Text>
                </View>
              </TouchableOpacity>
 textcol: {
    fontSize: 12,
    marginLeft: "75%",
    fontWeight: "300",
    textAlign: "center",
    alignItems: "center",
    alignSelf: "center",
    marginTop: "2%",
    color: "white",
    fontStyle: "italic",
  },

I want to create a button where if you click on it you copy the color of the said button.
But I need to click twice in order to copy my color (the first time I copy nothing)
Can someone help me or tell me where I made the error?
Here is my code :

  const [couleur, setCouleur] = useState("");
  async function cop() {
    await navigator.clipboard.writeText(couleur);
    alert("Couleur copiée");
  }

           <TouchableOpacity
                onPress={() => {
                  setCouleur("black");
                  cop();
                }}
              >
                <View
                  style={{
                    width: "100%",
                    height: 30,
                    backgroundColor: "#ADDAD4",
                  }}
                >
                  <Text style={styles.textcol}>green-8</Text>
                </View>
              </TouchableOpacity>
 textcol: {
    fontSize: 12,
    marginLeft: "75%",
    fontWeight: "300",
    textAlign: "center",
    alignItems: "center",
    alignSelf: "center",
    marginTop: "2%",
    color: "white",
    fontStyle: "italic",
  },

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

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

发布评论

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

评论(1

昔日梦未散 2025-02-06 06:32:44

您需要做2件事:

          <TouchableOpacity
            onPress={() => {
              setCouleur("#black");
            }}
          >

以及在开始时添加使用效率:

  useEffect(() => {
if (couleur !== "") {
  cop();
} },[couleur]);

You need to do 2 things:

          <TouchableOpacity
            onPress={() => {
              setCouleur("#black");
            }}
          >

as well as add an UseEffect at the beggining:

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