gpt4 book ai didi

reactjs - 我在 react 函数组件中使用 React.useRef 时出错

转载 作者:行者123 更新时间:2023-12-04 14:37:23 25 4
gpt4 key购买 nike

我在 react 功能组件中使用 React.useRef,在我在类组件中使用它之前我收到一条错误消息,现在我已将其更改为 react 功能组件,但我仍然在下面收到此错误消息。

React Hook "React.useRef" is called in function "landingPage" which is neither a React function component or a custom React Hook function



您能否解释一下为什么我在放入功能组件后仍然收到此消息。这是我的代码
import React from "react";
import Modal from "./Modal";

function landingPage() {
const modalRef = React.useRef();

return (
<div className="landingPage">
<div className="container">
<div className="landingPage__row">
<div className="playvideo-wrapper">
<div className="playvideo-text">See us in action</div>
<div className="playvideo-button" onClick={openModal}>
<p>Play Video</p>
</div>
<Modal ref={modalRef}>
<iframe
src="https://www.youtube.com/embed/SQ8CvT25_Dg?autoplay=1"
frameborder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
></iframe>
</Modal>
</div>
</div>
</div>
</div>
);
}

export default landingPage;


这是我在 app.js 中的代码
import React, { Component } from "react";
import LandingPage from "./LandingPage";

export default class App extends Component {
render() {
return (
<div className="main">
<LandingPage />
</div>
);
}
}

最佳答案

React 组件必须以大写字母开头。改变这个:

function landingPage() {...

对此:
function LandingPage() {...

关于reactjs - 我在 react 函数组件中使用 React.useRef 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60480162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com