0

I've just started using create-react-app and am not very familiar with the src setup. I noticed that in App.js, the exported App component is a function, as opposed to class App extends React.Component...

I can't seem to add a constructor to it. Anyone know where I can do so? Thank you

1
  • 1
    functional components do not have a constructor, only class based components can have a constructor defined in them. Commented Jan 1, 2020 at 13:01

4 Answers 4

3

You can change the function to a class as:


import ReactDOM from 'react-dom';
import React, { Component } from 'react';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: 'App.js'
    }
  }

  render() {
    return (
      <div>
          {this.state.text}
      </div>
    )
  }
}

export default App

Sign up to request clarification or add additional context in comments.

Comments

0

CRA team updated the setup to new React hooks. You are talking about old react setup. You can convert it

import React, { useState } from 'react';

function Example() {
  // Declare a new state variable, which we'll call "count"
  const [count, setCount] = useState(0);

  return (

To

class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = {date: new Date()};
  }

  render() {

But i will suggest you to learn react hooks to keep your skills updated.

Comments

0

As App is a functional component it doesn't have the component lifecycle. Therefore you can't specify a constructor.

But with class Based Component you can use the constructor

class App extends React.Component {
 constructor(props) {
    super(props);

 }

 render(){
 return(
 // code
 //code
 )}
}

Comments

0

Just use useEffect within the function.

Like:

React.useEffect(() => {
    // Whatever you like
    console.log("Called in starting of function");
  }, []);

5 Comments

unlike a constructor, useEffect hook written like this will run after every render cycle. This hook cannot be used as a constructor.
But the solution is to provide an alternate path which can be similar to constructor. @Yousaf In functional components this can be closest solution similar to constructor in classes.
No, it cannot be used as a constructor. It will run after every render cycle which is not like or similar to constructor at all.
@Yousaf You can use the useEffect hook as a constructor but you have to pass the second param as an empty array [] then it will only be run once when the component mounts. Ex: useEffect(() = alert('once'), [])
@Pablo not correct. When you pass empty array as second argument in useEffect hook, you emulate componentDidMount lifecycle method, not a constructor. Constructor executes before component is rendered whereas componentDidMount executes after component has been rendered. You cannot use useEffect component as a constructor and you don't need to inside a functional component

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.