DEV Community

Discussion on: Learn React in Plain English

Collapse
 
cariehl profile image
Cooper Riehl

Great article, with some quality information!

I'm new to React, and I've seen some other articles present Components as functions:

function MyComponent(props) {
  return (
    <div>
      <h1>Hello, world!</h1>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

What is the difference between writing Components this way, versus writing them as classes? Should I just pick my favorite style and write all my Components that way, or are there pitfalls I will run into with one method versus the other?

Thanks!

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
ivan_jrmc profile image
Ivan Jeremic

Well... you cannot say they are the same because they are not, I recommend learning function components only because this is the way React ecosystem goes, so don't learn class components it is a waste of time if you don't need them. Modern React uses function components only.

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
 
cariehl profile image
Cooper Riehl

@ivanjeremic Thanks for the comment! Could you give me an example where I might need to use a class component instead of a function component? Are class components only necessary when I want to implement additional functionality for the component, such as (not sure if this is the right term to use) React hooks?

Thread Thread
 
ivan_jrmc profile image
Ivan Jeremic

React hooks are used only in function components and do not work in class components. So all you need is to learn how hooks work.

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
 
ivan_jrmc profile image
Ivan Jeremic • Edited

Anything specific you want me to read? I read this page already. If you mean functions and class components are the same they are not, in the page you sent me it says react does't care from react's point of view a component is a component which is true but not from the developers point of view if you write components as classes they are totally different then functions and you cannot use hooks in class components.