2

So I'm trying to learn ReactJs( I have an AngularJs Background) and Im having trouble following a tutorial.

To create the project we run this command:

npx create-react-app app-name

Then after that in the tutorial the App.js is class. But when I run that command mine is a function.

Tutorial App.js

class App extends Component {
render() {
return (
  <div className="App">
    <h1>Hello, world!</h1>
  </div>
);
}
}

My App.js

function App() {
return (
<div className="App">

</div>
);
}

I'm so confused in this part.

code.cycling
  • 1,246
  • 2
  • 15
  • 33

2 Answers2

3

There are different ways to write that same: function component and class component.
Where class component supports the whole lifecycle of React component, function component is actually only the render function.
You can learn about the differences here:
https://reactjs.org/docs/components-and-props.html#function-and-class-components

In the last versions of React a new feature called Hooks came out, which let you do everything you need in function component: https://reactjs.org/docs/hooks-intro.html

Hope this helps :)

Naor
  • 23,465
  • 48
  • 152
  • 268
0

React components can be classes or can be functions. It works for both. The advantage with using class components is that it gives your component a lot more functionality like state, lifecycle methods. These components are usually complex. On the other hand function components are pretty straight forward and basic to learn. You can also convert functional components to class components using this: https://reactjs.org/docs/state-and-lifecycle.html

Hope this helps!!

Harshit Agarwal
  • 2,308
  • 2
  • 15
  • 23