Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
React Convert Hooks To Class But Get Error

I want to convert react hooks component to class component but I get error.

HOOKS

import React, { useEffect, useRef } from "react";

const App = () => {
  const refValt = useRef(null);

  const myfuncValt = () => {
    console.log("222222222222222222222");
  };


  useEffect(() => {
    setTimeout(() => {
      refValt.current.click();
    }, 5000); //miliseconds
  }, []);

  return (
    <div>
      <div ref={refValt} onClick={myfuncValt}>Valt</div>
    </div>
  );
};

export default App;

CLASS

import React  from "react";

const myfuncValt = () => {
  console.log("222222222222222222222");
};

class App extends React.Component {
  constructor(props) {
    super(props);
    const refValt = React.createRef(null);
  }

  componentDidMount() {
    setTimeout(() => {
      this.refValt.current.click();
    }, 5000);
  }

  render() {
    return (
      <div>
        <div ref={this.refValt} onClick={myfuncValt}>
          Valt
        </div>
      </div>
    );
  }
}

export default App;

I get this error =

enter image description here

I try this.refValt.click(); But It doesn't working.

Actually I suspect the differences between createref vs useref.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This:

const refValt = React.createRef(null);

... needs to be this:

this.refValt = React.createRef(null);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!