简体   繁体   中英

When to use a class component and when to use a functional component

I'm new to react.When I go for an interview they ask me When to use a class component and when to use a functional component?.I do not know the answer to this question.I know the difference between the two.How best to answer this question?

In current post-hooks era the answer is simple: you always use function components.

Simply explain the difference. You said you know what the difference is, so tell them what it is.

Use class components when you need to keep component-level state or use lifecycle components. Functional components can now use state and some lifecycle methods with hooks though, so it's up to personal preference.

Functional components are considered easier to read and are less code than classes. They should be used in presentational components (small components dealing with how things look, not how things work).

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