React Native:Press上更改组件道具

发布于 2025-01-25 09:58:35 字数 896 浏览 2 评论 0原文

我试图更改单击的按钮的道具:

如果单击超过3次,我想将其禁用。

这是我的代码:

import React, { useState } from 'react';
import { Button, Text, View } from 'react-native';

const App = () => {
  const [pressedCount, setPressedCount] = useState(0);

  return (
    <View style={{ flex: 1, justifyContent: 'center' }}>
      <Text style={{ margin: 16 }}>
        {pressedCount > 0
          ? `The button was pressed ${pressedCount} times!`
          : 'The button isn\'t pressed yet'
        }
      </Text>
      <Button
        title='Press me'
        onPress={() => if (pressedCount>=3){Button.setState.({disabled:true})} else{setPressedCount(pressedCount+1)}}
      />
    </View>
  );
};

export default App;

我已经尝试了(persedCount&gt; = 3){button.props.disabled = true} else {setPressedCount(persedCount+1)}}},但相同。

关于如何正确执行此操作的任何建议?

Im trying to change the props of the button which is clicked :

if clicked more than 3 times, i want to disable it.

Here is my code :

import React, { useState } from 'react';
import { Button, Text, View } from 'react-native';

const App = () => {
  const [pressedCount, setPressedCount] = useState(0);

  return (
    <View style={{ flex: 1, justifyContent: 'center' }}>
      <Text style={{ margin: 16 }}>
        {pressedCount > 0
          ? `The button was pressed ${pressedCount} times!`
          : 'The button isn\'t pressed yet'
        }
      </Text>
      <Button
        title='Press me'
        onPress={() => if (pressedCount>=3){Button.setState.({disabled:true})} else{setPressedCount(pressedCount+1)}}
      />
    </View>
  );
};

export default App;

I've tried if (pressedCount>=3){Button.props.disabled=true} else{setPressedCount(pressedCount+1)}} but it's the same.

Any advice on how to properly do that?

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

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

发布评论

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

评论(1

虐人心 2025-02-01 09:58:35
<Button
  title="Press me"
  onPress={() => setPressedCount((prevState) => prevState + 1)}
  disabled={pressedCount > 3}
/>;
<Button
  title="Press me"
  onPress={() => setPressedCount((prevState) => prevState + 1)}
  disabled={pressedCount > 3}
/>;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文