admin管理员组文章数量:1410716
I am trying to use the react-use-gesture library, but can't even seem to get some simple hooks working. I'm trying to use the useScroll hook (eventually to get an animation running with react-spring), but when I bind it to my ponent, nothing happens.
import { useScroll} from 'react-use-gesture';
function App() {
const bind = useScroll((e) => {
e.scrolling ? console.log("I'm being scrolled") : console.log("I'm not being scrolled");
});
return (
<div className="App" {...bind()}>
// All of my other ponents in App
</div>
);
}
export default App;
I have a feeling I'm missing something obvious. Does anyone have any ideas?
I am trying to use the react-use-gesture library, but can't even seem to get some simple hooks working. I'm trying to use the useScroll hook (eventually to get an animation running with react-spring), but when I bind it to my ponent, nothing happens.
import { useScroll} from 'react-use-gesture';
function App() {
const bind = useScroll((e) => {
e.scrolling ? console.log("I'm being scrolled") : console.log("I'm not being scrolled");
});
return (
<div className="App" {...bind()}>
// All of my other ponents in App
</div>
);
}
export default App;
I have a feeling I'm missing something obvious. Does anyone have any ideas?
Share asked Mar 6, 2020 at 4:56 RogyBearRogyBear 932 silver badges10 bronze badges2 Answers
Reset to default 5Oddly enough, I wasn't able to get the useScroll
hook to work either, but the useWheel
hook works just fine.
import React from "react";
import { useWheel } from "react-use-gesture";
import "./styles.css";
export default function App() {
const wheel = useWheel(state => {
console.log("wheeling", state.wheeling);
});
return (
<div className="App" {...wheel()}>
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
Was curious as to why scroll events weren't getting picked up but the mouse events were. Maybe this sheds a bit of light on the matter.
EDIT
Was able to get the scrolling working by passing optional config object, in this case, setting the DOM target to window
. According to Options explained it is remended to use an effect hook versus spreading in as a prop (though it worked in the codesandbox spread in).
export default function App() {
const scroll = useScroll(state => {
console.log("scrolling", state.scrolling);
}, {
domTarget: window,
});
useEffect(scroll, [scroll]);
return (
<div className="App" >
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
You just need to set overflow
property to scroll
in your container's style, otherwise no scroll event beside window's will be trigger.
import { useScroll } from 'react-use-gesture';
function App() {
const bind = useScroll((e) => {
e.scrolling ? console.log("I'm being scrolled") : console.log("I'm not being scrolled");
});
return (
<div className="App" style={{overflow: 'scroll'}} {...bind()}>
// All of my other ponents in App
</div>
);
}
export default App;
本文标签: javascriptreactusegesture useScroll not detecting scroll eventStack Overflow
版权声明:本文标题:javascript - react-use-gesture useScroll not detecting scroll event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745016960a2637902.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论