我设法react-intersection-observer
在功能组件中使用。但是,将它与类组件一起使用时我没有成功。我尝试按照文档进行操作,但出现以下错误:
Invariant failed: react-intersection-observer: No DOM node found. Make sure you forward "ref" to the root DOM element you want to observe.
我究竟做错了什么?先感谢您。
伪代码
- 当元素在视口中可见时触发动画。
import React, { Component } from "react";
import { Container, Row, Col } from "react-grid-system";
import { motion } from "framer-motion";
import { InView } from "react-intersection-observer";
let years, days, hours, mins, secs;
function upTime(countTo) {
var now = new Date();
countTo = new Date(countTo);
var difference = now - countTo;
years = Math.floor(difference / (60 * 60 * 1000 * 24) / 365);
days = Math.floor(difference / (60 * 60 * 1000 * 24));
hours = Math.floor((difference / (60 * 60 * 1000 * 24)) * 24);
mins = Math.floor((difference / (60 * 60 * 1000 * 24)) * 24 * 60);
secs = mins * 60;
clearTimeout(upTime.to);
upTime.to = setTimeout(function() {
upTime(countTo);
}, 1000);
}
export default class Timer extends Component {
state = {
y: 0,
d: 0,
h: 0,
m: 0,
s: 0,
toggle: true
};
componentDidMount() {
// Set inital seconds
upTime("may,05,2006,00:00:00");
let currentSec = secs;
this.myInterval = setInterval(() => {
upTime("may,05,2006,00:00:00");
currentSec += 1;
this.setState({
y: years,
d: days,
h: hours,
m: mins,
s: currentSec
});
}, 1000);
}
componentWillUnmount() {
clearInterval(this.myInterval);
}
render() {
const { y, h, m, s } = this.state;
const style = {
listStyle: "none",
fontFamily: "Soleil",
fontSize: "200px",
lineHeight: "220px",
color: "black",
textAlign: "left"
};
// Animation list
const list = {
visible: {
opacity: 1,
transition: { staggerChildren: 0.06 }
},
hidden: {
opacity: 0,
transition: {}
}
};
// Animation list
const item = {
visible: {
opacity: 1,
y: 0,
transition: { ease: "backInOut", duration: 0.8 }
},
hidden: { opacity: 0, y: 100 }
};
// Conditional render
if (y !== 0) {
return (
<Container fluid>
<Row>
<Col offset={{ xl: 1 }}>
<InView>
{({ inView, ref, entry }) => (
<motion.ul
ref={ref}
style={style}
initial="hidden"
animate={inView ? "visible" : "hidden"}
variants={list}
>
<motion.li variants={item}>{y}Y</motion.li>
<motion.li variants={item}>{h}H</motion.li>
<motion.li variants={item}>{m}M</motion.li>
<motion.li variants={item}>{s}</motion.li>
</motion.ul>
)}
</InView>
</Col>
</Row>
</Container>
);
} else {
return <div>"Loading..."</div>;
}
}
}