I have a component A that renders once the application loads.
I am looking to render another component B the moment component A leaves
the screen. This happens when the user scrolls component A off screen.
And also when user scrolls component A back into the screen, component B should
disappear.
Looking for guidance on how I could track whether component A is on the screen or off screen in React.
Please advice.
Idea is below. Looking for a way to listen if component A is on screen or not and setShowB(false) or setShowB(true) accordingly.
import React, { useState } from 'react';
const AComponent = () => {
const [showB, setShowB] = useState(false); // I do want this to be a boolean
const setVisibility = (showB) => showB ? 'visible' : 'hidden'
return (
<div>
<div id='A' style={{ visibility: setVisibility(showB)}}>
I am component A
</div>
<div id='B'>
I am component B
</div>
</div>
);
};
export default AComponent;
Note: Not looking to use any external libraries.
On page load only Component A shows.
===================================================================
when component A is scrolled off screen component B shows up.

