我在React函数组件中使用React.useRef时遇到错误

时间:2020-03-01 21:57:33

标签: reactjs react-hooks use-ref

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

反应挂钩“ React.useRef”在“ landingPage”函数中调用,该函数既不是React函数组件也不是自定义的React Hook函数

您能否解释一下为什么将功能部件放入后仍收到此消息。这是我的代码

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>
    );
  }
}

1 个答案:

答案 0 :(得分:8)

反应组件必须以大写字母开头。更改此:

function landingPage() {...

对此:

function LandingPage() {...