为什么我需要在TouchableOpacity上单击两次?
我想创建一个按钮,如果您单击它,则复制所述按钮的颜色。 但是我需要单击两次才能复制颜色(我第一次什么都不复制) 有人可以帮我还是告诉我在哪里犯错? 这是我的代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要做2件事:
以及在开始时添加使用效率:
You need to do 2 things:
as well as add an UseEffect at the beggining: