Skip to content

Commit 9d1e4cf

Browse files
authored
Merge pull request #301 from athithyaramaa1/patch-1
Updated answer to when to use functional vs class components. made th…
2 parents 69ec0ae + 7860daf commit 9d1e4cf

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

README.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,17 @@
543543
544544
1. If you need a React functionality whose Function component equivalent is not present yet, like Error Boundaries.
545545
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.
546557

547558

548559
**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,7 +573,7 @@
562573

563574
**[⬆ Back to Top](#table-of-contents)**
564575

565-
7. ### What are Pure Components?
576+
8. ### What are Pure Components?
566577

567578
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.
568579

@@ -610,7 +621,7 @@
610621
611622
**[⬆ Back to Top](#table-of-contents)**
612623
613-
8. ### What is state in React?
624+
9. ### What is state in React?
614625
615626
_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.
616627
@@ -663,7 +674,7 @@
663674
664675
**[⬆ Back to Top](#table-of-contents)**
665676
666-
9. ### What are props in React?
677+
10. ### What are props in React?
667678
668679
_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.
669680

0 commit comments

Comments
 (0)