我试图在js文件中的一个元素上使用useParallax钩子。我在使用NextJ和ReactJ以及样式化的组件。我使用钩子的方式如下:
Mainland.js
import React, { useEffect, useRef } from 'react';
import styled, { keyframes } from 'styled-components';
import { useParallax } from 'react-scroll-parallax';
const Mainland = () => {
const { parallaxRef } = useParallax({ speed: 20 }); // set up the hook to use with useRef
const StyledDiv = styled.div`` //Styled Component
return (
<StyledDiv ref={parallaxRef}>
...
</StyledDiv>这里的错误如下:
错误:您必须将useParallax()钩子返回的ref分配给一个HTML。
因此,我尝试使用它,没有样式的组件,直接到一个HTML元素,但它仍然没有工作。
附加信息:,我在_app.js文件中使用视差提供程序的方式如下:
import Layout from '../Components/Layout';
import '../styles/globals.css';
import { ParallaxProvider } from 'react-scroll-parallax';
function MyApp({ Component, pageProps, ...appProps }) {
const isLayoutNeeded = [`/Contact`].includes(appProps.router.pathname);
return (
<>
<Layout state={isLayoutNeeded}>
<ParallaxProvider>
<Component {...pageProps} />
</ParallaxProvider>
</Layout>
</>
);
}
export default MyApp;附加信息:https://react-scroll-parallax.damnthat.tv/docs/usage/hooks/use-parallax
发布于 2022-10-20 11:20:00
在项目文件夹中安装模块。帮我修好了
npm i install react-scroll-parallax发布于 2022-08-17 20:09:29
useParallax钩子返回{ ref, controller, element }。
所以试试这个:
const { ref: parallaxRef } = useParallax({ speed: 20 });https://stackoverflow.com/questions/73393745
复制相似问题