Skip to content

Commit 409217c

Browse files
authored
Merge pull request #294 from MarufZak/master
Explain the reason for using React.portal
2 parents 9d1e4cf + a08b3fe commit 409217c

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

README.md

+36-35
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,8 @@
16081608
16091609
48. ### What are portals in React?
16101610
1611-
_Portal_ is a recommended way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
1611+
_Portal_ is a recommended way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. When using
1612+
CSS transform in a component, its descendant elements should not use fixed positioning, otherwise the layout will blow up.
16121613
16131614
```javascript
16141615
ReactDOM.createPortal(child, container);
@@ -1618,13 +1619,13 @@
16181619
16191620
**[⬆ Back to Top](#table-of-contents)**
16201621
1621-
49. ### What are stateless components?
1622+
50. ### What are stateless components?
16221623
16231624
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.
16241625
16251626
**[⬆ Back to Top](#table-of-contents)**
16261627
1627-
50. ### What are stateful components?
1628+
51. ### What are stateful components?
16281629
16291630
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_.
16301631
@@ -1676,7 +1677,7 @@
16761677
16771678
**[⬆ Back to Top](#table-of-contents)**
16781679
1679-
51. ### How to apply validation on props in React?
1680+
52. ### How to apply validation on props in React?
16801681
16811682
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`.
16821683
@@ -1741,7 +1742,7 @@
17411742
17421743
**[⬆ Back to Top](#table-of-contents)**
17431744
1744-
52. ### What are the advantages of React?
1745+
53. ### What are the advantages of React?
17451746
17461747
Below are the list of main advantages of React,
17471748
@@ -1753,7 +1754,7 @@
17531754
17541755
**[⬆ Back to Top](#table-of-contents)**
17551756
1756-
53. ### What are the limitations of React?
1757+
54. ### What are the limitations of React?
17571758
17581759
Apart from the advantages, there are few limitations of React too,
17591760
@@ -1765,7 +1766,7 @@
17651766
17661767
**[⬆ Back to Top](#table-of-contents)**
17671768
1768-
54. ### What are error boundaries in React v16?
1769+
55. ### What are error boundaries in React v16?
17691770
17701771
_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.
17711772
@@ -1808,19 +1809,19 @@
18081809
18091810
**[⬆ Back to Top](#table-of-contents)**
18101811
1811-
55. ### How are error boundaries handled in React v15?
1812+
56. ### How are error boundaries handled in React v15?
18121813
18131814
React v15 provided very basic support for _error boundaries_ using `unstable_handleError` method. It has been renamed to `componentDidCatch` in React v16.
18141815
18151816
**[⬆ Back to Top](#table-of-contents)**
18161817
1817-
56. ### What are the recommended ways for static type checking?
1818+
57. ### What are the recommended ways for static type checking?
18181819
18191820
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.
18201821
18211822
**[⬆ Back to Top](#table-of-contents)**
18221823
1823-
57. ### What is the use of `react-dom` package?
1824+
58. ### What is the use of `react-dom` package?
18241825
18251826
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:
18261827
@@ -1832,7 +1833,7 @@
18321833
18331834
**[⬆ Back to Top](#table-of-contents)**
18341835
1835-
58. ### What is the purpose of render method of `react-dom`?
1836+
59. ### What is the purpose of render method of `react-dom`?
18361837
18371838
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.
18381839
@@ -1844,7 +1845,7 @@
18441845
18451846
**[⬆ Back to Top](#table-of-contents)**
18461847
1847-
59. ### What is ReactDOMServer?
1848+
60. ### What is ReactDOMServer?
18481849
18491850
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:
18501851
@@ -1871,7 +1872,7 @@
18711872
18721873
**[⬆ Back to Top](#table-of-contents)**
18731874
1874-
60. ### How to use innerHTML in React?
1875+
61. ### How to use innerHTML in React?
18751876
18761877
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.
18771878
@@ -1889,7 +1890,7 @@
18891890
18901891
**[⬆ Back to Top](#table-of-contents)**
18911892
1892-
61. ### How to use styles in React?
1893+
62. ### How to use styles in React?
18931894
18941895
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.
18951896
@@ -1908,7 +1909,7 @@
19081909
19091910
**[⬆ Back to Top](#table-of-contents)**
19101911
1911-
62. ### How events are different in React?
1912+
63. ### How events are different in React?
19121913
19131914
Handling events in React elements has some syntactic differences:
19141915
@@ -1917,13 +1918,13 @@
19171918
19181919
**[⬆ Back to Top](#table-of-contents)**
19191920
1920-
63. ### What will happen if you use `setState()` in constructor?
1921+
64. ### What will happen if you use `setState()` in constructor?
19211922
19221923
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.
19231924
19241925
**[⬆ Back to Top](#table-of-contents)**
19251926
1926-
64. ### What is the impact of indexes as keys?
1927+
65. ### What is the impact of indexes as keys?
19271928
19281929
Keys should be stable, predictable, and unique so that React can keep track of elements.
19291930
@@ -1945,7 +1946,7 @@
19451946
19461947
**[⬆ Back to Top](#table-of-contents)**
19471948
1948-
65. ### Is it good to use `setState()` in `componentWillMount()` method?
1949+
66. ### Is it good to use `setState()` in `componentWillMount()` method?
19491950
19501951
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()`.
19511952
@@ -1962,7 +1963,7 @@
19621963
19631964
**[⬆ Back to Top](#table-of-contents)**
19641965
1965-
66. ### What will happen if you use props in initial state?
1966+
67. ### What will happen if you use props in initial state?
19661967
19671968
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.
19681969
@@ -2005,7 +2006,7 @@
20052006
20062007
**[⬆ Back to Top](#table-of-contents)**
20072008
2008-
67. ### How do you conditionally render components?
2009+
68. ### How do you conditionally render components?
20092010
20102011
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.
20112012
@@ -2031,7 +2032,7 @@
20312032
20322033
**[⬆ Back to Top](#table-of-contents)**
20332034
2034-
68. ### Why we need to be careful when spreading props on DOM elements?
2035+
69. ### Why we need to be careful when spreading props on DOM elements?
20352036
20362037
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.
20372038
@@ -2049,7 +2050,7 @@
20492050
20502051
**[⬆ Back to Top](#table-of-contents)**
20512052
2052-
69. ### How you use decorators in React?
2053+
70. ### How you use decorators in React?
20532054
20542055
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.
20552056
@@ -2081,7 +2082,7 @@
20812082
20822083
**[⬆ Back to Top](#table-of-contents)**
20832084
2084-
70. ### How do you memoize a component?
2085+
71. ### How do you memoize a component?
20852086
20862087
There are memoize libraries available which can be used on function components.
20872088
@@ -2113,7 +2114,7 @@
21132114
21142115
**[⬆ Back to Top](#table-of-contents)**
21152116
2116-
71. ### How you implement Server Side Rendering or SSR?
2117+
72. ### How you implement Server Side Rendering or SSR?
21172118
21182119
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.
21192120
@@ -2128,13 +2129,13 @@
21282129
21292130
**[⬆ Back to Top](#table-of-contents)**
21302131
2131-
72. ### How to enable production mode in React?
2132+
73. ### How to enable production mode in React?
21322133
21332134
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.
21342135
21352136
**[⬆ Back to Top](#table-of-contents)**
21362137
2137-
73. ### What is CRA and its benefits?
2138+
74. ### What is CRA and its benefits?
21382139
21392140
The `create-react-app` CLI tool allows you to quickly create & run React applications with no configuration step.
21402141
@@ -2165,7 +2166,7 @@
21652166
21662167
**[⬆ Back to Top](#table-of-contents)**
21672168
2168-
74. ### What is the lifecycle methods order in mounting?
2169+
75. ### What is the lifecycle methods order in mounting?
21692170
21702171
The lifecycle methods are called in the following order when an instance of a component is being created and inserted into the DOM.
21712172
@@ -2176,7 +2177,7 @@
21762177

21772178
**[⬆ Back to Top](#table-of-contents)**
21782179

2179-
75. ### What are the lifecycle methods going to be deprecated in React v16?
2180+
76. ### What are the lifecycle methods going to be deprecated in React v16?
21802181

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

@@ -2188,7 +2189,7 @@
21882189

21892190
**[⬆ Back to Top](#table-of-contents)**
21902191

2191-
76. ### What is the purpose of `getDerivedStateFromProps()` lifecycle method?
2192+
77. ### What is the purpose of `getDerivedStateFromProps()` lifecycle method?
21922193

21932194
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.
21942195

@@ -2204,7 +2205,7 @@
22042205
22052206
**[⬆ Back to Top](#table-of-contents)**
22062207
2207-
77. ### What is the purpose of `getSnapshotBeforeUpdate()` lifecycle method?
2208+
78. ### What is the purpose of `getSnapshotBeforeUpdate()` lifecycle method?
22082209
22092210
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()`.
22102211
@@ -2220,13 +2221,13 @@
22202221
22212222
**[⬆ Back to Top](#table-of-contents)**
22222223
2223-
78. ### Do Hooks replace render props and higher order components?
2224+
79. ### Do Hooks replace render props and higher order components?
22242225
22252226
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.
22262227
22272228
**[⬆ Back to Top](#table-of-contents)**
22282229
2229-
79. ### What is the recommended way for naming components?
2230+
80. ### What is the recommended way for naming components?
22302231
22312232
It is recommended to name the component by reference instead of using `displayName`.
22322233
@@ -2258,7 +2259,7 @@
22582259
22592260
**[⬆ Back to Top](#table-of-contents)**
22602261
2261-
80. ### What is the recommended ordering of methods in component class?
2262+
81. ### What is the recommended ordering of methods in component class?
22622263
22632264
_Recommended_ ordering of methods from _mounting_ to _render stage_:
22642265
@@ -2279,7 +2280,7 @@
22792280

22802281
**[⬆ Back to Top](#table-of-contents)**
22812282

2282-
81. ### What is a switching component?
2283+
82. ### What is a switching component?
22832284

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

@@ -2312,7 +2313,7 @@
23122313
23132314
**[⬆ Back to Top](#table-of-contents)**
23142315
2315-
82. ### Why we need to pass a function to setState()?
2316+
83. ### Why we need to pass a function to setState()?
23162317
23172318
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()`.
23182319

0 commit comments

Comments
 (0)