【问题标题】:Detecting scroll direction using hooks使用钩子检测滚动方向
【发布时间】:2021-08-30 18:31:49
【问题描述】:

这是我的代码:

const [scollPosition, setSrollPosition] = useState(0);

const handleScroll = () => {
  const position = window.pageYOffset;
  console.log(position);
  if (position > scollPosition) {
    console.log("down");
  } else {
    console.log("up");
  }
  setSrollPosition(position);
};

useEffect(() => {
  window.addEventListener("scroll", handleScroll, { passive: true });
  return () => window.removeEventListener("scroll", handleScroll);
}, []);

但这不起作用,因为setSrollPosition(position)没有更新scollPositon的值。

【问题讨论】:

标签: scroll react-hooks


【解决方案1】:

这是 Reactjs 状态的本质,它是异步更新的

对于你的情况,请改用useRef(),这样你就不需要关心上面的事情了:

const scollPosition = useRef(0);

  const handleScroll = () => {
    const position = window.pageYOffset;
    console.log(position);
    if (position > scollPosition.current) {
      console.log("down");
    } else {
      console.log("up");
    }
    scollPosition.current = position;
  };

  useEffect(() => {
    window.addEventListener("scroll", handleScroll, { passive: true });
    return () => window.removeEventListener("scroll", handleScroll);
  }, []);

工作示例:

function App() {
  const scollPosition = React.useRef(0);

  const handleScroll = () => {
    const position = window.pageYOffset;
    console.log(position);
    if (position > scollPosition.current) {
      console.log("down");
    } else {
      console.log("up");
    }
    scollPosition.current = position;
  };

  React.useEffect(() => {
    window.addEventListener("scroll", handleScroll, { passive: true });
    return () => window.removeEventListener("scroll", handleScroll);
  }, []);
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div>
<div id="root"></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 2020-10-11
    • 1970-01-01
    • 2021-07-08
    相关资源
    最近更新 更多