Skip to content

Commit 55f906e

Browse files
committed
2 parents 18826d2 + 409217c commit 55f906e

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

README.md

+50-38
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
@@ -1598,7 +1609,8 @@
15981609
15991610
48. ### What are portals in React?
16001611
1601-
_Portal_ is a recommended way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
1612+
_Portal_ is a recommended way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. When using
1613+
CSS transform in a component, its descendant elements should not use fixed positioning, otherwise the layout will blow up.
16021614
16031615
```javascript
16041616
ReactDOM.createPortal(child, container);
@@ -1608,13 +1620,13 @@
16081620
16091621
**[⬆ Back to Top](#table-of-contents)**
16101622
1611-
49. ### What are stateless components?
1623+
50. ### What are stateless components?
16121624
16131625
If the behaviour of a component is independent of its state then it can be a stateless component. You can use either a function or a class for creating stateless components. But unless you need to use a lifecycle hook in your components, you should go for function components. There are a lot of benefits if you decide to use function components here; they are easy to write, understand, and test, a little faster, and you can avoid the `this` keyword altogether.
16141626
16151627
**[⬆ Back to Top](#table-of-contents)**
16161628
1617-
50. ### What are stateful components?
1629+
51. ### What are stateful components?
16181630
16191631
If the behaviour of a component is dependent on the _state_ of the component then it can be termed as stateful component. These _stateful components_ are either function components with hooks or _class components_.
16201632
@@ -1666,7 +1678,7 @@
16661678
16671679
**[⬆ Back to Top](#table-of-contents)**
16681680
1669-
51. ### How to apply validation on props in React?
1681+
52. ### How to apply validation on props in React?
16701682
16711683
When the application is running in _development mode_, React will automatically check all props that we set on components to make sure they have _correct type_. If the type is incorrect, React will generate warning messages in the console. It's disabled in _production mode_ due to performance impact. The mandatory props are defined with `isRequired`.
16721684
@@ -1731,7 +1743,7 @@
17311743
17321744
**[⬆ Back to Top](#table-of-contents)**
17331745
1734-
52. ### What are the advantages of React?
1746+
53. ### What are the advantages of React?
17351747
17361748
Below are the list of main advantages of React,
17371749
@@ -1743,7 +1755,7 @@
17431755
17441756
**[⬆ Back to Top](#table-of-contents)**
17451757
1746-
53. ### What are the limitations of React?
1758+
54. ### What are the limitations of React?
17471759
17481760
Apart from the advantages, there are few limitations of React too,
17491761
@@ -1755,7 +1767,7 @@
17551767
17561768
**[⬆ Back to Top](#table-of-contents)**
17571769
1758-
54. ### What are error boundaries in React v16?
1770+
55. ### What are error boundaries in React v16?
17591771
17601772
_Error boundaries_ are components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed.
17611773
@@ -1798,19 +1810,19 @@
17981810
17991811
**[⬆ Back to Top](#table-of-contents)**
18001812
1801-
55. ### How are error boundaries handled in React v15?
1813+
56. ### How are error boundaries handled in React v15?
18021814
18031815
React v15 provided very basic support for _error boundaries_ using `unstable_handleError` method. It has been renamed to `componentDidCatch` in React v16.
18041816
18051817
**[⬆ Back to Top](#table-of-contents)**
18061818
1807-
56. ### What are the recommended ways for static type checking?
1819+
57. ### What are the recommended ways for static type checking?
18081820
18091821
Normally we use _PropTypes library_ (`React.PropTypes` moved to a `prop-types` package since React v15.5) for _type checking_ in the React applications. For large code bases, it is recommended to use _static type checkers_ such as Flow or TypeScript, that perform type checking at compile time and provide auto-completion features.
18101822
18111823
**[⬆ Back to Top](#table-of-contents)**
18121824
1813-
57. ### What is the use of `react-dom` package?
1825+
58. ### What is the use of `react-dom` package?
18141826
18151827
The `react-dom` package provides _DOM-specific methods_ that can be used at the top level of your app. Most of the components are not required to use this module. Some of the methods of this package are:
18161828
@@ -1822,7 +1834,7 @@
18221834
18231835
**[⬆ Back to Top](#table-of-contents)**
18241836
1825-
58. ### What is the purpose of render method of `react-dom`?
1837+
59. ### What is the purpose of render method of `react-dom`?
18261838
18271839
This method is used to render a React element into the DOM in the supplied container and return a reference to the component. If the React element was previously rendered into container, it will perform an update on it and only mutate the DOM as necessary to reflect the latest changes.
18281840
@@ -1834,7 +1846,7 @@
18341846
18351847
**[⬆ Back to Top](#table-of-contents)**
18361848
1837-
59. ### What is ReactDOMServer?
1849+
60. ### What is ReactDOMServer?
18381850
18391851
The `ReactDOMServer` object enables you to render components to static markup (typically used on node server). This object is mainly used for _server-side rendering_ (SSR). The following methods can be used in both the server and browser environments:
18401852
@@ -1861,7 +1873,7 @@
18611873
18621874
**[⬆ Back to Top](#table-of-contents)**
18631875
1864-
60. ### How to use innerHTML in React?
1876+
61. ### How to use innerHTML in React?
18651877
18661878
The `dangerouslySetInnerHTML` attribute is React's replacement for using `innerHTML` in the browser DOM. Just like `innerHTML`, it is risky to use this attribute considering cross-site scripting (XSS) attacks. You just need to pass a `__html` object as key and HTML text as value.
18671879
@@ -1879,7 +1891,7 @@
18791891
18801892
**[⬆ Back to Top](#table-of-contents)**
18811893
1882-
61. ### How to use styles in React?
1894+
62. ### How to use styles in React?
18831895
18841896
The `style` attribute accepts a JavaScript object with camelCased properties rather than a CSS string. This is consistent with the DOM style JavaScript property, is more efficient, and prevents XSS security holes.
18851897
@@ -1898,7 +1910,7 @@
18981910
18991911
**[⬆ Back to Top](#table-of-contents)**
19001912
1901-
62. ### How events are different in React?
1913+
63. ### How events are different in React?
19021914
19031915
Handling events in React elements has some syntactic differences:
19041916
@@ -1907,13 +1919,13 @@
19071919
19081920
**[⬆ Back to Top](#table-of-contents)**
19091921
1910-
63. ### What will happen if you use `setState()` in constructor?
1922+
64. ### What will happen if you use `setState()` in constructor?
19111923
19121924
When you use `setState()`, then apart from assigning to the object state React also re-renders the component and all its children. You would get error like this: _Can only update a mounted or mounting component._ So we need to use `this.state` to initialize variables inside constructor.
19131925
19141926
**[⬆ Back to Top](#table-of-contents)**
19151927
1916-
64. ### What is the impact of indexes as keys?
1928+
65. ### What is the impact of indexes as keys?
19171929
19181930
Keys should be stable, predictable, and unique so that React can keep track of elements.
19191931
@@ -1936,7 +1948,7 @@
19361948
19371949
**[⬆ Back to Top](#table-of-contents)**
19381950
1939-
65. ### Is it good to use `setState()` in `componentWillMount()` method?
1951+
66. ### Is it good to use `setState()` in `componentWillMount()` method?
19401952
19411953
Yes, it is safe to use `setState()` inside `componentWillMount()` method. But at the same it is recommended to avoid async initialization in `componentWillMount()` lifecycle method. `componentWillMount()` is invoked immediately before mounting occurs. It is called before `render()`, therefore setting state in this method will not trigger a re-render. Avoid introducing any side-effects or subscriptions in this method. We need to make sure async calls for component initialization happened in `componentDidMount()` instead of `componentWillMount()`.
19421954
@@ -1953,7 +1965,7 @@
19531965
19541966
**[⬆ Back to Top](#table-of-contents)**
19551967
1956-
66. ### What will happen if you use props in initial state?
1968+
67. ### What will happen if you use props in initial state?
19571969
19581970
If the props on the component are changed without the component being refreshed, the new prop value will never be displayed because the constructor function will never update the current state of the component. The initialization of state from props only runs when the component is first created.
19591971
@@ -1996,7 +2008,7 @@
19962008
19972009
**[⬆ Back to Top](#table-of-contents)**
19982010
1999-
67. ### How do you conditionally render components?
2011+
68. ### How do you conditionally render components?
20002012
20012013
In some cases you want to render different components depending on some state. JSX does not render `false` or `undefined`, so you can use conditional _short-circuiting_ to render a given part of your component only if a certain condition is true.
20022014
@@ -2022,7 +2034,7 @@
20222034
20232035
**[⬆ Back to Top](#table-of-contents)**
20242036
2025-
68. ### Why we need to be careful when spreading props on DOM elements?
2037+
69. ### Why we need to be careful when spreading props on DOM elements?
20262038
20272039
When we _spread props_ we run into the risk of adding unknown HTML attributes, which is a bad practice. Instead we can use prop destructuring with `...rest` operator, so it will add only required props.
20282040
@@ -2040,7 +2052,7 @@
20402052
20412053
**[⬆ Back to Top](#table-of-contents)**
20422054
2043-
69. ### How you use decorators in React?
2055+
70. ### How you use decorators in React?
20442056
20452057
You can _decorate_ your _class_ components, which is the same as passing the component into a function. **Decorators** are flexible and readable way of modifying component functionality.
20462058
@@ -2072,7 +2084,7 @@
20722084
20732085
**[⬆ Back to Top](#table-of-contents)**
20742086
2075-
70. ### How do you memoize a component?
2087+
71. ### How do you memoize a component?
20762088
20772089
There are memoize libraries available which can be used on function components.
20782090
@@ -2104,7 +2116,7 @@
21042116
21052117
**[⬆ Back to Top](#table-of-contents)**
21062118
2107-
71. ### How you implement Server Side Rendering or SSR?
2119+
72. ### How you implement Server Side Rendering or SSR?
21082120
21092121
React is already equipped to handle rendering on Node servers. A special version of the DOM renderer is available, which follows the same pattern as on the client side.
21102122
@@ -2119,13 +2131,13 @@
21192131
21202132
**[⬆ Back to Top](#table-of-contents)**
21212133
2122-
72. ### How to enable production mode in React?
2134+
73. ### How to enable production mode in React?
21232135
21242136
You should use Webpack's `DefinePlugin` method to set `NODE_ENV` to `production`, by which it strip out things like propType validation and extra warnings. Apart from this, if you minify the code, for example, Uglify's dead-code elimination to strip out development only code and comments, it will drastically reduce the size of your bundle.
21252137
21262138
**[⬆ Back to Top](#table-of-contents)**
21272139
2128-
73. ### What is CRA and its benefits?
2140+
74. ### What is CRA and its benefits?
21292141
21302142
The `create-react-app` CLI tool allows you to quickly create & run React applications with no configuration step.
21312143
@@ -2156,7 +2168,7 @@
21562168
21572169
**[⬆ Back to Top](#table-of-contents)**
21582170
2159-
74. ### What is the lifecycle methods order in mounting?
2171+
75. ### What is the lifecycle methods order in mounting?
21602172
21612173
The lifecycle methods are called in the following order when an instance of a component is being created and inserted into the DOM.
21622174
@@ -2167,7 +2179,7 @@
21672179

21682180
**[⬆ Back to Top](#table-of-contents)**
21692181

2170-
75. ### What are the lifecycle methods going to be deprecated in React v16?
2182+
76. ### What are the lifecycle methods going to be deprecated in React v16?
21712183

21722184
The following lifecycle methods going to be unsafe coding practices and will be more problematic with async rendering.
21732185

@@ -2179,7 +2191,7 @@
21792191

21802192
**[⬆ Back to Top](#table-of-contents)**
21812193

2182-
76. ### What is the purpose of `getDerivedStateFromProps()` lifecycle method?
2194+
77. ### What is the purpose of `getDerivedStateFromProps()` lifecycle method?
21832195

21842196
The new static `getDerivedStateFromProps()` lifecycle method is invoked after a component is instantiated as well as before it is re-rendered. It can return an object to update state, or `null` to indicate that the new props do not require any state updates.
21852197

@@ -2195,7 +2207,7 @@
21952207
21962208
**[⬆ Back to Top](#table-of-contents)**
21972209
2198-
77. ### What is the purpose of `getSnapshotBeforeUpdate()` lifecycle method?
2210+
78. ### What is the purpose of `getSnapshotBeforeUpdate()` lifecycle method?
21992211
22002212
The new `getSnapshotBeforeUpdate()` lifecycle method is called right before DOM updates. The return value from this method will be passed as the third parameter to `componentDidUpdate()`.
22012213
@@ -2211,13 +2223,13 @@
22112223
22122224
**[⬆ Back to Top](#table-of-contents)**
22132225
2214-
78. ### Do Hooks replace render props and higher order components?
2226+
79. ### Do Hooks replace render props and higher order components?
22152227
22162228
Both render props and higher-order components render only a single child but in most of the cases Hooks are a simpler way to serve this by reducing nesting in your tree.
22172229
22182230
**[⬆ Back to Top](#table-of-contents)**
22192231
2220-
79. ### What is the recommended way for naming components?
2232+
80. ### What is the recommended way for naming components?
22212233
22222234
It is recommended to name the component by reference instead of using `displayName`.
22232235
@@ -2249,7 +2261,7 @@
22492261
22502262
**[⬆ Back to Top](#table-of-contents)**
22512263
2252-
80. ### What is the recommended ordering of methods in component class?
2264+
81. ### What is the recommended ordering of methods in component class?
22532265
22542266
_Recommended_ ordering of methods from _mounting_ to _render stage_:
22552267
@@ -2270,7 +2282,7 @@
22702282

22712283
**[⬆ Back to Top](#table-of-contents)**
22722284

2273-
81. ### What is a switching component?
2285+
82. ### What is a switching component?
22742286

22752287
A _switching component_ is a component that renders one of many components. We need to use object to map prop values to components.
22762288

@@ -2303,7 +2315,7 @@
23032315
23042316
**[⬆ Back to Top](#table-of-contents)**
23052317
2306-
82. ### Why we need to pass a function to setState()?
2318+
83. ### Why we need to pass a function to setState()?
23072319
23082320
The reason behind for this is that `setState()` is an asynchronous operation. React batches state changes for performance reasons, so the state may not change immediately after `setState()` is called. That means you should not rely on the current state when calling `setState()` since you can't be sure what that state will be. The solution is to pass a function to `setState()`, with the previous state as an argument. By doing this you can avoid issues with the user getting the old state value on access due to the asynchronous nature of `setState()`.
23092321

0 commit comments

Comments
 (0)