账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    react里点击方法效果实现延迟
    41
    0

    下面是一个简单的分页功能:

    pagination.jsx

    render() {
            return (
                <div className="pagination">
                    <span className=" tab prev">《</span>
                    {
                        this.props.tabArr.map((item, index) => {
                            return (
                                <span key={index} className="tab" data-index={index} onClick={this.props.tabClick.bind(this,index)}>{index + 1}</span>
                            );
                        })
                    }
                    <span className="next">next ></span>
                </div>
            );
        }

    index.jsx

    //import pagination.jsx
    
     tabClick(num) {
    
        // const index = e.target.getAttribute('data-index');
        const index =num;
        console.log(index);  
        const arr = this.state.allDataArr;
        this.setState({
          startShowNum: index * 8,
          showArr: arr.slice(this.state.startShowNum, (this.state.startShowNum + 8))
    
        });
      }

    问题:为什么tabClick方法里的index是跟着我点击时改变的,但是showArr(要展示的数组)却要延迟,当我点击第二次才会生效?

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 在React中,你可以使用setTimeoutsetInterval函数来实现点击事件的延迟。这两个函数都会在给定的时间后执行一个函数。

      例如,假设你有一个点击事件,你需要让它在鼠标离开该元素后延迟3秒,你可以这样做:

      ```jsx import React, { useState } from 'react';

      function MyComponent() { const [isClicked, setIsClicked] = useState(false);

      const handleClick = () => { setIsClicked(true); setTimeout(() => { setIsClicked(false); }, 3000); //延迟3秒 };

      return (

      {isClicked ?

      You clicked me.

      : null}
      ); }

      export default MyComponent; ```

      在这个例子中,handleClick函数在鼠标离开按钮后立即调用,而setTimeout函数会在3秒后调用。这样,当鼠标离开按钮时,点击事件会被延迟,而不是立即执行。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部