|
543 | 543 |
|
544 | 544 | 1. If you need a React functionality whose Function component equivalent is not present yet, like Error Boundaries.
|
545 | 545 | 2. In older versions, If the component needs _state or lifecycle methods_ then you need to use class component.
|
| 546 | + |
| 547 | + So the summary to this question is as follows: |
| 548 | + **Use Function Components:** |
| 549 | + |
| 550 | + - If you don't need state or lifecycle methods, and your component is purely presentational. |
| 551 | + - For simplicity, readability, and modern code practices, especially with the use of React Hooks for state and side effects. |
| 552 | + |
| 553 | + **Use Class Components:** |
| 554 | + |
| 555 | + - If you need to manage state or use lifecycle methods. |
| 556 | + - In scenarios where backward compatibility or integration with older code is necessary. |
546 | 557 |
|
547 | 558 |
|
548 | 559 | **Note:** You can also use reusable [react error boundary](https://github.com/bvaughn/react-error-boundary) third-party component without writing any class. i.e, No need to use class components for Error boundaries.
|
|
562 | 573 |
|
563 | 574 | **[⬆ Back to Top](#table-of-contents)**
|
564 | 575 |
|
565 |
| -7. ### What are Pure Components? |
| 576 | +8. ### What are Pure Components? |
566 | 577 |
|
567 | 578 | Pure components are the components which render the same output for the same state and props. In function components, you can achieve these pure components through memoized `React.memo()` API wrapping around the component. This API prevents unnecessary re-renders by comparing the previous props and new props using shallow comparison. So it will be helpful for performance optimizations.
|
568 | 579 |
|
|
610 | 621 |
|
611 | 622 | **[⬆ Back to Top](#table-of-contents)**
|
612 | 623 |
|
613 |
| -8. ### What is state in React? |
| 624 | +9. ### What is state in React? |
614 | 625 |
|
615 | 626 | _State_ of a component is an object that holds some information that may change over the lifetime of the component. The important point is whenever the state object changes, the component re-renders. It is always recommended to make our state as simple as possible and minimize the number of stateful components.
|
616 | 627 |
|
|
663 | 674 |
|
664 | 675 | **[⬆ Back to Top](#table-of-contents)**
|
665 | 676 |
|
666 |
| -9. ### What are props in React? |
| 677 | +10. ### What are props in React? |
667 | 678 |
|
668 | 679 | _Props_ are inputs to components. They are single values or objects containing a set of values that are passed to components on creation similar to HTML-tag attributes. Here, the data is passed down from a parent component to a child component.
|
669 | 680 |
|
|
0 commit comments