I don't know why the handleScroll only executes once, not when i keep scrolling up and down. And I need to somehow get the element's height but im not sure how other than documentElement. The function needs to turn a true/false so that I can do a setState and add/change a class in my heroGallery div for a css animation.
import React, { Component } from "react";
class Intro extends Component {
constructor(props) {
super(props);
this.heroRef = React.createRef();
this.state = {};
}
componentDidMount = () => {
this.heroRef.current.getBoundingClientRect();
let hero2 = this.heroRef;
console.log(this.heroRef.current);
window.addEventListener("scroll", this.handleScroll(hero2));
};
componentWillUnmount = () => {
window.removeEventListener("scroll", this.handleScroll);
};
handleScroll = elm => {
var rect = elm.current.getBoundingClientRect();
//var viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
//return !(rect.bottom < 0 || rect.top - viewHeight >= 0);
};
render() {
return (
<div className="introCont">
<div className="introFirstSection" />
<div className="heroSection">
<div className="heroGallery" ref={this.heroRef}>
<div className="slide-down">item 1</div>
<div>item 2</div>
<div>item 3</div>
<div>item 4</div>
<div>item 5</div>
<div>item 6</div>
<div>item 7</div>
<div>item 8</div>
</div>
</div>
</div>
);
}
}
export default Intro;
EDIT: Taking out my intro component from Switch lets it work.
import React, { lazy, Suspense, Component } from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import { connect } from "react-redux";
import * as actions from "./actions";
//import asyncComponent from "./components/AsyncComponent";
//const Header = asyncComponent(() => import("./Header"));
import Intro from "./components/Intro";
const Header = lazy(() => import("./components/Header"));
const Footer = lazy(() => import("./components/Footer"));
const Landing = lazy(() => import("./components/Landing"));
const Profile = lazy(() => import("./components/Profile"));
const BookList = lazy(() => import("./components/books/BookList"));
const BookNote = lazy(() => import("./components/books/BookNote"));
const StatsChart = lazy(() => import("./components/StatsChart"));
class App extends Component {
componentDidMount() {
this.props.fetchUser();
}
render() {
return (
<BrowserRouter>
<div className="rootdk">
<Suspense fallback={<div />}>
<Header />
</Suspense>
<div className="container">
<Suspense fallback={<div className="loader" />}>
<Switch>
<Route exact path="/intro" component={() => <Intro />} />
<Route
exact
path="/mybooks"
component={() =>
this.props.thisuser ? <BookList /> : <Landing />
}
/>
<Route
exact
path="/booknotes"
component={() =>
this.props.thisuser ? <BookNote /> : <Landing />
}
/>
<Route
exact
path="/statschart"
component={() =>
this.props.thisuser ? <StatsChart /> : <Landing />
}
/>
<Route
exact
path="/profile"
component={() =>
this.props.thisuser ? <Profile /> : <Landing />
}
/>
<Route exact path="/" component={Landing} />
</Switch>
</Suspense>
<Suspense fallback={<div />}>
<Footer />
</Suspense>
</div>
</div>
</BrowserRouter>
);
}
}
function mapStateToProps(props) {
return { thisuser: props.auth };
}
export default connect(
mapStateToProps,
actions
)(App);