【问题标题】:How to prevent a double click in ReactJS如何防止 ReactJS 中的双击
【发布时间】:2019-08-29 22:27:35
【问题描述】:

我正在研究 ReactJS 中的搜索过滤器,但遇到了一个问题。问题是当用户进行一些搜索并想要单击next(因为我在应用程序中有分页)然后用户将单击两次以加载其他数据页面。

我需要避免这种行为:我的意思是当用户单击 next 时,它应该是“加载数据”而不是“双击”。

我是 ReactJS 新手,请专家帮助我

代码

    btnClick() {
        const { Item,skip , filtered, data } = this.state
        if(filtered.length>0 || !data.length ){
          window.alert("Hello,")
          this.setState({
            filtered:[],
            skip:0
          },()=>this.getData());
          return false
        }else {
          window.alert("Hello , Dear")
        this.setState(
          {
            Item,
            skip: skip + pageSize
          },
          () => this.getData()
        );}

      }

【问题讨论】:

标签: javascript reactjs ecmascript-6


【解决方案1】:

您可以拥有isLoading 状态并在此状态下设置disabled 上的disabled 属性,这样在获取数据时将不允许再次单击按钮。

 btnClick() {
   const {
     Item,
     skip,
     filtered,
     data
   } = this.state;
   
   if (filtered.length > 0 || !data.length) {
     window.alert("Hello,")
     this.setState({
       filtered: [],
       skip: 0
     }, () => this.fetchData());
     return false
     
   } else {
     window.alert("Hello , Dear")
     this.setState({
         Item,
         skip: skip + pageSize
       },
       () => this.fetchData()
     );
   }
 }
 
 fetchData() = async() => {
     this.setState({ isLoading: true });
     
     await this.getData();
     
     this.setState({ isLoading: false });
 }
 
 render() {
        const {
      isLoading
    } = this.state;
    
    const buttonProps = isLoading ? { disabled: true} ? {};
    
    return (
      <button onClick={this.btnClick} { ...buttonProps }>
            Click to fetch
      </button>
    );
 }

【讨论】:

    猜你喜欢
    • 2012-06-01
    • 2017-07-19
    • 2018-12-25
    • 1970-01-01
    • 2022-01-21
    • 2012-04-24
    • 2015-12-21
    • 2019-02-05
    • 2011-09-11
    相关资源
    最近更新 更多