简体   繁体   English

React Native:将按钮绑定到 onPress 功能并随后禁用

[英]React Native: Bind button to onPress function and disable afterwards

I have an infinity amount of buttons in my app due to I load items from my database and add a button to each item and the amount of items variates each time.由于我从数据库加载项目并为每个项目添加一个按钮,并且项目数量每次都不同,因此我的应用程序中有无限数量的按钮。 This is the reason why I cannot use the state to this.这就是我不能使用状态的原因。

However, I want to disable the button after the button got pressed.但是,我想在按下按钮后禁用该按钮。 So it should only be possible to press the button once.所以应该只能按一次按钮。 This is my (React Native Elements) button so far:到目前为止,这是我的(React Native Elements)按钮:

<Button
  onPress={() =>
    this.upvote(product.id, this.onPress.bind(this))
  }
  style={{
    width: Dimensions.get("window").width / 2
  }}
  buttonStyle={styles.productVotingButton}
  title={config.voteButton}
/>

And this here is my upvote function:这是我的投票功能:

upvote(id, btn) {
    btn.setAttribute("disabled", "disabled");
  }

I have to give the upvote function the id of my item I want to upvote.我必须为 upvote 函数提供我想要投票的项目的 ID。 So, it is necessary!因此,有必要! However, I want to disable the button after the button got pressed ones.但是,我想在按钮被按下后禁用该按钮。 The problem I am facing here is I don't know how to pass the button component to the function so I can disable exactly the button which got pressed and not any other button...我在这里面临的问题是我不知道如何将按钮组件传递给函数,所以我可以完全禁用被按下的按钮而不是任何其他按钮......

Would appreciate any kind of help!将不胜感激任何形式的帮助!

One option is to maintain a list of already-clicked product IDs in your state.一种选择是维护您所在州已点击的产品 ID 列表。 Then you only need to pass the button ID to the handler - when the button is clicked check if the ID is in the list and either do nothing or perform the action and update the list of clicked IDs.然后您只需要将按钮 ID 传递给处理程序 - 单击按钮时检查 ID 是否在列表中,然后不执行任何操作或执行操作并更新单击的 ID 列表。

An advantage of doing it this way is that you can pre-fill that list with the products the user has already upvoted so they can't do it again by reloading the page (assuming such data is stored on your backend and is available over some API), and you can also use the list to disable buttons if the ID is already in the list.这样做的一个好处是,您可以使用用户已经投票的产品预先填充该列表,这样他们就无法通过重新加载页面来再次执行此操作(假设此类数据存储在您的后端并且在某些情况下可用) API),如果 ID 已经在列表中,您也可以使用列表来禁用按钮。 Something like...就像是...

const renderButton: (product, state) => (
    const { likedIds } = state;
    <Button
        onClick={() => this.upvote(product.id)}
        disabled={likedIds.indexOf(product.id) >= 0}
        ...
    />
);

const upvote = (productId) => {
    const { likedIds } = this.state;
    if (likedIds.indexOf(productId) >= 0) {
        return;
    } else {
        // whatever you need to do
        this.setState({
            likedIds: [...likedIds, productId]
        });
    }
};

I haven't tested this, but it should give you a rough idea.我没有测试过这个,但它应该给你一个粗略的想法。

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

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