我正在尝试使用 react-intersection-observer 更改滚动时的背景视频。当 inView 更改为 true 时,useEffect 必须将 state 更改为 sample2,并将新视频发送到 startscreen,它用作固定位置的背景视频。状态在变,但视频还是一样。
//开始屏幕
const Startscreen = ({ sample }) => {
console.log(sample);
return (
<video className="videoTag" autoPlay loop muted>
<source src={sample} type="video/mp4" />
</video>
);
};
//App.js
import sample1 from "./videos/0.mov";
import sample2 from "./videos/2.mov";
function App() {
const [state, setState] = useState(sample1);
const { ref, inView, entry } = useInView({
threshold: 0.5,
});
useEffect(() => {
if (inView === true) {
setState(sample2);
} else {
setState(sample1);
}
console.log(state);
}, [inView]);
return (
<div className="App">
<Startscreen sample={state} />
<div ref={ref}>
<AboutMe>
</div>
</div>
)};