addEventListener('scroll') to scrollable using useRef - ReactScroll to bottom of div?How to Check if element is visible after scrolling?Creating a div element in jQueryScroll to the top of the page using JavaScript/jQuery?addEventListener vs onclickjQuery scroll to elementLoop inside React JSXWhat do these three dots in React do?Programmatically navigate using react routerHow can you remove a Jquery Document Click Listeners Effecting React Element?
Locked-up DOS computer beeped on keypress. What mechanism caused that?
You have no, but can try for yes
What's a German word for »Sandbagger«?
"Je suis petite, moi?", purpose of the "moi"?
Why does a tetrahedral molecule like methane have a dipole moment of zero?
What is a Kravchuk transform and how is it related to Fourier transforms?
Somebody hacked my clock
Do higher dimensions have axes?
Do Australia and New Zealand have a travel ban on Somalis (like Wikipedia says)?
Does a hash function have a Upper bound on input length?
Authorship dispute on a paper that came out of a final report of a course?
Is it possible to target 2 allies with the Warding Bond spell using the Sorcerer's Twinned Spell metamagic option?
When we are talking about black hole evaporation - what exactly happens?
What is the intuition for higher homotopy groups not vanishing?
Could a US citizen born through "birth tourism" become President?
Dative single noun Bankautomaten?
Why isn't a binary file shown as 0s and 1s?
Making a Dataset that emulates `ls -tlra`?
Discontinuous Tube visualization
Applying for jobs with an obvious scar
Should I work for free if client's requirement changed
Which modern firearm should a time traveler bring to be easily reproducible for a historic civilization?
What is the function of "mal" in saying "Das nenn ich mal ein X"?
How to determine Platform Event Size in Apex to ensure to be within < 1 MB
addEventListener('scroll') to scrollable
using useRef - React
Scroll to bottom of div?How to Check if element is visible after scrolling?Creating a div element in jQueryScroll to the top of the page using JavaScript/jQuery?addEventListener vs onclickjQuery scroll to elementLoop inside React JSXWhat do these three dots in React do?Programmatically navigate using react routerHow can you remove a Jquery Document Click Listeners Effecting React Element?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This is one of the first times I am actually using React Hooks properly in a project so bear with me if I am not quite there.
In the component below, my aim is to display the <HelperTooltip>
on load and when the scrolling div (not the window) scrolls I want to hide after it scrolls X amount of pixels.
My thought process is to create a useRef object on the scrolling <div/>
element, which then I can add an event listens with a callback function which then can toggle the state to hide the <HelperTooltip>
I have created a Codesandbox below to try and demonstrate what I am trying to do. As you can see in the demo the node.addEventListener('click')
is working fine, however when I try and call the node.addEventListener('scroll')
it is not firing.
I'm not sure if I taking the wrong approach or not, any help will greatly be appreciated. In the codesandbox demo it is the react image that I trying to hide on scroll, not the <HelperTooltip>
CodeSandbox link: https://codesandbox.io/s/zxj322ln24
import React, useRef, useCallback, useState from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const App = props =>
const [isLogoActive, toggleLogo] = useState(true);
const scrollElementRef = useCallback(node =>
node.addEventListener("click", event =>
console.log("clicked", event);
);
/*
I want to add the scroll event listener
here and the set the state isLogoActive to
false like the event listener above but the 'scroll' event
is firing --- see below on line 21
*/
// node.addEventListener("scroll", event =>
// console.log("scrolled", event);
// toggle log
// );
);
return (
<div className="scrolling-container">
<div ref=scrollElementRef className="scrolling-element">
<p>top</p>
isLogoActive && (
<div className="element-to-hide-after-scroll">
<img
style= width: "100px", height: "100px"
src="https://arcweb.co/wp-content/uploads/2016/10/react-logo-1000-transparent-768x768.png"
/>
</div>
)
<p>bottom</p>
</div>
</div>
);
;
ReactDOM.render(<App />, document.getElementById("app"));
javascript reactjs typescript react-hooks
add a comment |
This is one of the first times I am actually using React Hooks properly in a project so bear with me if I am not quite there.
In the component below, my aim is to display the <HelperTooltip>
on load and when the scrolling div (not the window) scrolls I want to hide after it scrolls X amount of pixels.
My thought process is to create a useRef object on the scrolling <div/>
element, which then I can add an event listens with a callback function which then can toggle the state to hide the <HelperTooltip>
I have created a Codesandbox below to try and demonstrate what I am trying to do. As you can see in the demo the node.addEventListener('click')
is working fine, however when I try and call the node.addEventListener('scroll')
it is not firing.
I'm not sure if I taking the wrong approach or not, any help will greatly be appreciated. In the codesandbox demo it is the react image that I trying to hide on scroll, not the <HelperTooltip>
CodeSandbox link: https://codesandbox.io/s/zxj322ln24
import React, useRef, useCallback, useState from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const App = props =>
const [isLogoActive, toggleLogo] = useState(true);
const scrollElementRef = useCallback(node =>
node.addEventListener("click", event =>
console.log("clicked", event);
);
/*
I want to add the scroll event listener
here and the set the state isLogoActive to
false like the event listener above but the 'scroll' event
is firing --- see below on line 21
*/
// node.addEventListener("scroll", event =>
// console.log("scrolled", event);
// toggle log
// );
);
return (
<div className="scrolling-container">
<div ref=scrollElementRef className="scrolling-element">
<p>top</p>
isLogoActive && (
<div className="element-to-hide-after-scroll">
<img
style= width: "100px", height: "100px"
src="https://arcweb.co/wp-content/uploads/2016/10/react-logo-1000-transparent-768x768.png"
/>
</div>
)
<p>bottom</p>
</div>
</div>
);
;
ReactDOM.render(<App />, document.getElementById("app"));
javascript reactjs typescript react-hooks
maybe you can use useEffect hook instead of useCallback
– duc mai
Mar 26 at 11:45
add a comment |
This is one of the first times I am actually using React Hooks properly in a project so bear with me if I am not quite there.
In the component below, my aim is to display the <HelperTooltip>
on load and when the scrolling div (not the window) scrolls I want to hide after it scrolls X amount of pixels.
My thought process is to create a useRef object on the scrolling <div/>
element, which then I can add an event listens with a callback function which then can toggle the state to hide the <HelperTooltip>
I have created a Codesandbox below to try and demonstrate what I am trying to do. As you can see in the demo the node.addEventListener('click')
is working fine, however when I try and call the node.addEventListener('scroll')
it is not firing.
I'm not sure if I taking the wrong approach or not, any help will greatly be appreciated. In the codesandbox demo it is the react image that I trying to hide on scroll, not the <HelperTooltip>
CodeSandbox link: https://codesandbox.io/s/zxj322ln24
import React, useRef, useCallback, useState from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const App = props =>
const [isLogoActive, toggleLogo] = useState(true);
const scrollElementRef = useCallback(node =>
node.addEventListener("click", event =>
console.log("clicked", event);
);
/*
I want to add the scroll event listener
here and the set the state isLogoActive to
false like the event listener above but the 'scroll' event
is firing --- see below on line 21
*/
// node.addEventListener("scroll", event =>
// console.log("scrolled", event);
// toggle log
// );
);
return (
<div className="scrolling-container">
<div ref=scrollElementRef className="scrolling-element">
<p>top</p>
isLogoActive && (
<div className="element-to-hide-after-scroll">
<img
style= width: "100px", height: "100px"
src="https://arcweb.co/wp-content/uploads/2016/10/react-logo-1000-transparent-768x768.png"
/>
</div>
)
<p>bottom</p>
</div>
</div>
);
;
ReactDOM.render(<App />, document.getElementById("app"));
javascript reactjs typescript react-hooks
This is one of the first times I am actually using React Hooks properly in a project so bear with me if I am not quite there.
In the component below, my aim is to display the <HelperTooltip>
on load and when the scrolling div (not the window) scrolls I want to hide after it scrolls X amount of pixels.
My thought process is to create a useRef object on the scrolling <div/>
element, which then I can add an event listens with a callback function which then can toggle the state to hide the <HelperTooltip>
I have created a Codesandbox below to try and demonstrate what I am trying to do. As you can see in the demo the node.addEventListener('click')
is working fine, however when I try and call the node.addEventListener('scroll')
it is not firing.
I'm not sure if I taking the wrong approach or not, any help will greatly be appreciated. In the codesandbox demo it is the react image that I trying to hide on scroll, not the <HelperTooltip>
CodeSandbox link: https://codesandbox.io/s/zxj322ln24
import React, useRef, useCallback, useState from "react";
import ReactDOM from "react-dom";
import "./styles.css";
const App = props =>
const [isLogoActive, toggleLogo] = useState(true);
const scrollElementRef = useCallback(node =>
node.addEventListener("click", event =>
console.log("clicked", event);
);
/*
I want to add the scroll event listener
here and the set the state isLogoActive to
false like the event listener above but the 'scroll' event
is firing --- see below on line 21
*/
// node.addEventListener("scroll", event =>
// console.log("scrolled", event);
// toggle log
// );
);
return (
<div className="scrolling-container">
<div ref=scrollElementRef className="scrolling-element">
<p>top</p>
isLogoActive && (
<div className="element-to-hide-after-scroll">
<img
style= width: "100px", height: "100px"
src="https://arcweb.co/wp-content/uploads/2016/10/react-logo-1000-transparent-768x768.png"
/>
</div>
)
<p>bottom</p>
</div>
</div>
);
;
ReactDOM.render(<App />, document.getElementById("app"));
javascript reactjs typescript react-hooks
javascript reactjs typescript react-hooks
asked Mar 26 at 11:34
Sam KelhamSam Kelham
2472 gold badges4 silver badges18 bronze badges
2472 gold badges4 silver badges18 bronze badges
maybe you can use useEffect hook instead of useCallback
– duc mai
Mar 26 at 11:45
add a comment |
maybe you can use useEffect hook instead of useCallback
– duc mai
Mar 26 at 11:45
maybe you can use useEffect hook instead of useCallback
– duc mai
Mar 26 at 11:45
maybe you can use useEffect hook instead of useCallback
– duc mai
Mar 26 at 11:45
add a comment |
2 Answers
2
active
oldest
votes
An easier approach for this particular use case might be to use the onScroll
prop and use the scrollTop
property from the event target
to figure out if you should hide the image or not.
Example
const useState = React;
const App = props =>
const [isLogoActive, setLogoActive] = useState(true);
const onScroll = e =>
setLogoActive(e.target.scrollTop < 100);
;
return (
<div onScroll=onScroll style= height: 300, overflowY: "scroll" >
<p style= marginBottom: 200 >top</p>
<img
style=
width: 100,
height: 100,
visibility: isLogoActive ? "visible" : "hidden"
src="https://arcweb.co/wp-content/uploads/2016/10/react-logo-1000-transparent-768x768.png"
/>
<p style= marginTop: 200 >bottom</p>
</div>
);
;
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>
add a comment |
In your example, the scroll is not triggered on the scrolling-element
but on the scrolling-container
so that's where you want to put your ref : https://codesandbox.io/s/ko4vm93moo :)
But as Throlle said, you could also use the onScroll prop !
Yep great spot about putting the ref on the wrong element! and thank you for giving me a comprehensive answer, works a treat. I will have to do some reading on the useEffect hook and see what it is all about. Thank you
– Sam Kelham
Mar 26 at 22:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55356172%2faddeventlistenerscroll-to-scrollable-div-using-useref-react%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
An easier approach for this particular use case might be to use the onScroll
prop and use the scrollTop
property from the event target
to figure out if you should hide the image or not.
Example
const useState = React;
const App = props =>
const [isLogoActive, setLogoActive] = useState(true);
const onScroll = e =>
setLogoActive(e.target.scrollTop < 100);
;
return (
<div onScroll=onScroll style= height: 300, overflowY: "scroll" >
<p style= marginBottom: 200 >top</p>
<img
style=
width: 100,
height: 100,
visibility: isLogoActive ? "visible" : "hidden"
src="https://arcweb.co/wp-content/uploads/2016/10/react-logo-1000-transparent-768x768.png"
/>
<p style= marginTop: 200 >bottom</p>
</div>
);
;
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>
add a comment |
An easier approach for this particular use case might be to use the onScroll
prop and use the scrollTop
property from the event target
to figure out if you should hide the image or not.
Example
const useState = React;
const App = props =>
const [isLogoActive, setLogoActive] = useState(true);
const onScroll = e =>
setLogoActive(e.target.scrollTop < 100);
;
return (
<div onScroll=onScroll style= height: 300, overflowY: "scroll" >
<p style= marginBottom: 200 >top</p>
<img
style=
width: 100,
height: 100,
visibility: isLogoActive ? "visible" : "hidden"
src="https://arcweb.co/wp-content/uploads/2016/10/react-logo-1000-transparent-768x768.png"
/>
<p style= marginTop: 200 >bottom</p>
</div>
);
;
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>
add a comment |
An easier approach for this particular use case might be to use the onScroll
prop and use the scrollTop
property from the event target
to figure out if you should hide the image or not.
Example
const useState = React;
const App = props =>
const [isLogoActive, setLogoActive] = useState(true);
const onScroll = e =>
setLogoActive(e.target.scrollTop < 100);
;
return (
<div onScroll=onScroll style= height: 300, overflowY: "scroll" >
<p style= marginBottom: 200 >top</p>
<img
style=
width: 100,
height: 100,
visibility: isLogoActive ? "visible" : "hidden"
src="https://arcweb.co/wp-content/uploads/2016/10/react-logo-1000-transparent-768x768.png"
/>
<p style= marginTop: 200 >bottom</p>
</div>
);
;
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>
An easier approach for this particular use case might be to use the onScroll
prop and use the scrollTop
property from the event target
to figure out if you should hide the image or not.
Example
const useState = React;
const App = props =>
const [isLogoActive, setLogoActive] = useState(true);
const onScroll = e =>
setLogoActive(e.target.scrollTop < 100);
;
return (
<div onScroll=onScroll style= height: 300, overflowY: "scroll" >
<p style= marginBottom: 200 >top</p>
<img
style=
width: 100,
height: 100,
visibility: isLogoActive ? "visible" : "hidden"
src="https://arcweb.co/wp-content/uploads/2016/10/react-logo-1000-transparent-768x768.png"
/>
<p style= marginTop: 200 >bottom</p>
</div>
);
;
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>
const useState = React;
const App = props =>
const [isLogoActive, setLogoActive] = useState(true);
const onScroll = e =>
setLogoActive(e.target.scrollTop < 100);
;
return (
<div onScroll=onScroll style= height: 300, overflowY: "scroll" >
<p style= marginBottom: 200 >top</p>
<img
style=
width: 100,
height: 100,
visibility: isLogoActive ? "visible" : "hidden"
src="https://arcweb.co/wp-content/uploads/2016/10/react-logo-1000-transparent-768x768.png"
/>
<p style= marginTop: 200 >bottom</p>
</div>
);
;
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>
const useState = React;
const App = props =>
const [isLogoActive, setLogoActive] = useState(true);
const onScroll = e =>
setLogoActive(e.target.scrollTop < 100);
;
return (
<div onScroll=onScroll style= height: 300, overflowY: "scroll" >
<p style= marginBottom: 200 >top</p>
<img
style=
width: 100,
height: 100,
visibility: isLogoActive ? "visible" : "hidden"
src="https://arcweb.co/wp-content/uploads/2016/10/react-logo-1000-transparent-768x768.png"
/>
<p style= marginTop: 200 >bottom</p>
</div>
);
;
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div id="root"></div>
answered Mar 26 at 22:49
TholleTholle
50.2k8 gold badges63 silver badges86 bronze badges
50.2k8 gold badges63 silver badges86 bronze badges
add a comment |
add a comment |
In your example, the scroll is not triggered on the scrolling-element
but on the scrolling-container
so that's where you want to put your ref : https://codesandbox.io/s/ko4vm93moo :)
But as Throlle said, you could also use the onScroll prop !
Yep great spot about putting the ref on the wrong element! and thank you for giving me a comprehensive answer, works a treat. I will have to do some reading on the useEffect hook and see what it is all about. Thank you
– Sam Kelham
Mar 26 at 22:21
add a comment |
In your example, the scroll is not triggered on the scrolling-element
but on the scrolling-container
so that's where you want to put your ref : https://codesandbox.io/s/ko4vm93moo :)
But as Throlle said, you could also use the onScroll prop !
Yep great spot about putting the ref on the wrong element! and thank you for giving me a comprehensive answer, works a treat. I will have to do some reading on the useEffect hook and see what it is all about. Thank you
– Sam Kelham
Mar 26 at 22:21
add a comment |
In your example, the scroll is not triggered on the scrolling-element
but on the scrolling-container
so that's where you want to put your ref : https://codesandbox.io/s/ko4vm93moo :)
But as Throlle said, you could also use the onScroll prop !
In your example, the scroll is not triggered on the scrolling-element
but on the scrolling-container
so that's where you want to put your ref : https://codesandbox.io/s/ko4vm93moo :)
But as Throlle said, you could also use the onScroll prop !
answered Mar 26 at 14:49
antoinechalifourantoinechalifour
2851 silver badge8 bronze badges
2851 silver badge8 bronze badges
Yep great spot about putting the ref on the wrong element! and thank you for giving me a comprehensive answer, works a treat. I will have to do some reading on the useEffect hook and see what it is all about. Thank you
– Sam Kelham
Mar 26 at 22:21
add a comment |
Yep great spot about putting the ref on the wrong element! and thank you for giving me a comprehensive answer, works a treat. I will have to do some reading on the useEffect hook and see what it is all about. Thank you
– Sam Kelham
Mar 26 at 22:21
Yep great spot about putting the ref on the wrong element! and thank you for giving me a comprehensive answer, works a treat. I will have to do some reading on the useEffect hook and see what it is all about. Thank you
– Sam Kelham
Mar 26 at 22:21
Yep great spot about putting the ref on the wrong element! and thank you for giving me a comprehensive answer, works a treat. I will have to do some reading on the useEffect hook and see what it is all about. Thank you
– Sam Kelham
Mar 26 at 22:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55356172%2faddeventlistenerscroll-to-scrollable-div-using-useref-react%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
maybe you can use useEffect hook instead of useCallback
– duc mai
Mar 26 at 11:45