简体   繁体   中英

What type of components should I use in React: functional components or class base components?

What confused me is that both functional component and class based component can now use State and Props but in different implementations. I prefer class based component since I am angular developer and I find it more comfortable to work with these type of components. But I searched for this and many experts said it is better to use functional components as mush as possible.

I want to know if it really matters with tech companies when someone apply for react developer position. Would they look in these things? Since they differ a lot in implementations...

There are some differences between method component and class component .

Method Component:

The simplest way to declare a component in React. you need only to declare a method that returns a jsx . example :

const MessageComponent = ({ name }) => { return <h1>Hi {name}</h1> }

in addition to since introduction of React Hooks you can do most of functionalities using method component

componentDidUpdate => useEffect(fn)

componentDidMount => useEffect(fn, [])

state => useState()

Class Component:

the robust version of the component. With class component you can do more. Props will by default, in the class context this.props . You can use state, local variable for your component. You can add many class's method that share the same state.

export default class MessageComponent  extends Component {
  state = {
    message: 'default message'
  }

  renderMessage = () => {
    return (
      <h1>
        Hi {this.props.name}
      </h1>
    )
  }

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

Class Component VS Method Component

The major reason to not using class component is when you only need a simple component like a button , card , or representational component. If your component doesn't need a complex state, complex logic, method component is best for you.

Functional components is the new standard. It is easier to keep them light weight and readable. In high quality code no component should need so much logic that it is ever more than 30-50 lines tops which keeps things short and succinct, with this in mind a class component is just large and clunky

Class components were originally used, and are still supported by React. According to the React developers, there is no plan to stop supporting them. In an existing codebase there is no reason to spend resources converting class components to function components.

Having said that, function components are the better option for new code. They also support React Hooks which is a substitute for the class based lifecycle methods.

If you are writing new code, probably focus on writing function components. But if you are applying for jobs, there is a high chance that existing codebases have both class and function components. You will need to be comfortable with both and be able to translate one into the other.

Well organizations now tend to use the latest there is, and employ someone that resonates with whatever's new.

Fuctional Components with new Hooks that also replace lifecycle method are

  • Easier to manage
  • Greater compatibility , especially with typescript
  • Much Less and Cleaner Code

Nowadays, most of the developers are diverting to functional components instead of class components because there may be many reasons but some of the most beneficial points are

  1. They are more readable in comparison to class-based components.
  2. Easy to test and debug because it requires less code.
  3. As per the guideline, it may boost performance.

I suppose you should write new components as functional components. Because now on the companies will manage class-based components for older code but they will move on with functional components. Therefore, you should be aware of both the coding conventions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM