Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I got an error for using React.useRef inside a react function component

I am using React.useRef inside a react functional component, I am getting an error message before i was using it inside a class component and now i have changed it to a react functional component and I am still getting this error message below.

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

Could you please explain why am i still getting this message after placing in a functional component. Here is my code

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;

and here is my code in app.js

import React, { Component } from "react";
import LandingPage from "./LandingPage";

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

like image 966
Code Ninja Avatar asked Aug 24 '26 10:08

Code Ninja


2 Answers

React components MUST start with a capital letter. Change this:

function landingPage() {...

to this:

function LandingPage() {...
like image 140
Jayce444 Avatar answered Aug 26 '26 02:08

Jayce444


I had the same problem, always remember that every React component MUST start with a Capital letter,

Rename your

function landingPage(){...} to function LandingPage(){...}.

The Reason behind this is that state changes are done in classes, and so these will refer your functions as Classes, and for classes, the first letter of the class should be in Capital Letter.

like image 21
Anurag Anand Avatar answered Aug 26 '26 01:08

Anurag Anand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!