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

标签 reactjs react-hooks use-ref

我在 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/

相关文章:

reactjs - 如何在 React 中使用 Relax.js?

reactjs - 使用 ref react 打开选择文件对话框不起作用?

reactjs - MUI - 在 DataGrid 中禁用多行选择

javascript - React Native 动画 useEffect 钩子(Hook)与依赖项数组创建无限循环

reactjs - React Hook useEffect 缺少依赖项 : 'list'

reactjs - useState React 钩子(Hook)总是返回初始值

reactjs - ESLint 详尽-deps : ignore value that came from a ref

javascript - 如何处理复选框数组的状态?

javascript - 作为对象的一部分发送时无法读取 response.json()

javascript - 将额外的 props 传递给 React 组件