refactor LazyLoader and fix bug (#3121)

pull/3135/head
RobertJoonas 3 years ago committed by GitHub
parent c550a87e01
commit 49a2a14237
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,40 +1,22 @@
import React from 'react';
import React, { useEffect, useState } from 'react'
import { useInView } from 'react-intersection-observer'
export default class extends React.Component {
componentDidMount() {
if ('IntersectionObserver' in window) {
this.attachObserver()
} else {
this.props.onVisible && this.props.onVisible()
}
}
attachObserver() {
this.observer = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting) {
this.props.onVisible && this.props.onVisible()
this.observer.unobserve(this.element);
}
}, {
threshold: 0
});
export default function LazyLoader(props) {
const [hasBecomeVisibleYet, setHasBecomeVisibleYet] = useState(false)
const { ref, inView } = useInView({
threshold: 0,
})
this.observer.observe(this.element);
}
componentWillUnmount() {
this.observer && this.observer.unobserve(this.element);
}
useEffect(() => {
if (inView && !hasBecomeVisibleYet) {
setHasBecomeVisibleYet(true)
props.onVisible && props.onVisible()
}
}, [inView])
render() {
return (
<div
ref={(el) => { this.element = el }}
className={this.props.className}
style={this.props.style}
>
{this.props.children}
</div>
);
}
return (
<div ref={ref}>
{props.children}
</div>
)
}

@ -40,7 +40,7 @@ export default class Conversions extends React.Component {
componentDidUpdate(prevProps) {
if (this.props.query !== prevProps.query) {
const height = this.htmlNode.current.element.offsetHeight
const height = this.htmlNode.current.offsetHeight
this.setState({ loading: true, goals: null, prevHeight: height })
this.fetchConversions()
}
@ -108,9 +108,11 @@ export default class Conversions extends React.Component {
renderConversions() {
return (
<LazyLoader ref={this.htmlNode} style={{ minHeight: '132px', height: this.state.prevHeight ?? 'auto' }} onVisible={this.onVisible}>
{this.renderInner()}
</LazyLoader>
<div ref={this.htmlNode} style={{ minHeight: '132px', height: this.state.prevHeight ?? 'auto' }} >
<LazyLoader onVisible={this.onVisible}>
{this.renderInner()}
</LazyLoader>
</div>
)
}

@ -163,8 +163,8 @@ export default class Referrers extends React.Component {
render() {
return (
<div>
<LazyLoader onVisible={this.onVisible} className="flex flex-col flex-grow">
<div className="flex flex-col flex-grow">
<LazyLoader onVisible={this.onVisible}>
<h3 className="font-bold dark:text-gray-100">Top Referrers</h3>
{this.state.loading && <div className="mx-auto loading mt-44"><div></div></div>}
<FadeIn show={!this.state.loading}>

@ -116,8 +116,8 @@ class AllSources extends React.Component {
render() {
return (
<div>
<LazyLoader className="flex flex-col flex-grow" onVisible={this.onVisible}>
<div className="flex flex-col flex-grow">
<LazyLoader onVisible={this.onVisible}>
<div id="sources" className="flex justify-between w-full">
<h3 className="font-bold dark:text-gray-100">Top Sources</h3>
{this.props.renderTabs()}

@ -44,6 +44,7 @@
"react-dom": "^16.13.1",
"react-flatpickr": "3.10.5",
"react-flip-move": "^3.0.4",
"react-intersection-observer": "^9.5.2",
"react-popper": "^2.3.0",
"react-router-dom": "^5.2.0",
"react-transition-group": "^4.4.2",
@ -7665,6 +7666,14 @@
"react-dom": ">=16.3.x"
}
},
"node_modules/react-intersection-observer": {
"version": "9.5.2",
"resolved": "https://registry.npmjs.org/react-intersection-observer/-/react-intersection-observer-9.5.2.tgz",
"integrity": "sha512-EmoV66/yvksJcGa1rdW0nDNc4I1RifDWkT50gXSFnPLYQ4xUptuDD4V7k+Rj1OgVAlww628KLGcxPXFlOkkU/Q==",
"peerDependencies": {
"react": "^15.0.0 || ^16.0.0 || ^17.0.0 || ^18.0.0"
}
},
"node_modules/react-is": {
"version": "16.13.1",
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
@ -15319,6 +15328,12 @@
"integrity": "sha512-Mf4XpbkUNZy9eu80iXXFIjToDvw+bnHxmKHVoositbMpV87O/EQswnXUqVovRHoTx/F+4dE+p//PyJnAT7OtPA==",
"requires": {}
},
"react-intersection-observer": {
"version": "9.5.2",
"resolved": "https://registry.npmjs.org/react-intersection-observer/-/react-intersection-observer-9.5.2.tgz",
"integrity": "sha512-EmoV66/yvksJcGa1rdW0nDNc4I1RifDWkT50gXSFnPLYQ4xUptuDD4V7k+Rj1OgVAlww628KLGcxPXFlOkkU/Q==",
"requires": {}
},
"react-is": {
"version": "16.13.1",
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",

@ -46,6 +46,7 @@
"react-dom": "^16.13.1",
"react-flatpickr": "3.10.5",
"react-flip-move": "^3.0.4",
"react-intersection-observer": "^9.5.2",
"react-popper": "^2.3.0",
"react-router-dom": "^5.2.0",
"react-transition-group": "^4.4.2",

Loading…
Cancel
Save