Skip to content

Commit a08b3fe

Browse files
authored
Explain the reason for using React.portal
1 parent 52709ad commit a08b3fe

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
@@ -1568,7 +1568,8 @@
15681568
15691569
48. ### What are portals in React?
15701570
1571-
_Portal_ is a recommended way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
1571+
_Portal_ is a recommended way to render children into a DOM node that exists outside the DOM hierarchy of the parent component. When using
1572+
CSS transform in a component, its descendant elements should not use fixed positioning, otherwise the layout will blow up.
15721573
15731574
```javascript
15741575
ReactDOM.createPortal(child, container);
@@ -1578,13 +1579,13 @@
15781579
15791580
**[⬆ Back to Top](#table-of-contents)**
15801581
1581-
49. ### What are stateless components?
1582+
50. ### What are stateless components?
15821583
15831584
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.
15841585
15851586
**[⬆ Back to Top](#table-of-contents)**
15861587
1587-
50. ### What are stateful components?
1588+
51. ### What are stateful components?
15881589
15891590
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_.
15901591
@@ -1636,7 +1637,7 @@
16361637
16371638
**[⬆ Back to Top](#table-of-contents)**
16381639
1639-
51. ### How to apply validation on props in React?
1640+
52. ### How to apply validation on props in React?
16401641
16411642
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`.
16421643
@@ -1701,7 +1702,7 @@
17011702
17021703
**[⬆ Back to Top](#table-of-contents)**
17031704
1704-
52. ### What are the advantages of React?
1705+
53. ### What are the advantages of React?
17051706
17061707
Below are the list of main advantages of React,
17071708
@@ -1713,7 +1714,7 @@
17131714
17141715
**[⬆ Back to Top](#table-of-contents)**
17151716
1716-
53. ### What are the limitations of React?
1717+
54. ### What are the limitations of React?
17171718
17181719
Apart from the advantages, there are few limitations of React too,
17191720
@@ -1725,7 +1726,7 @@
17251726
17261727
**[⬆ Back to Top](#table-of-contents)**
17271728
1728-
54. ### What are error boundaries in React v16?
1729+
55. ### What are error boundaries in React v16?
17291730
17301731
_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.
17311732
@@ -1768,19 +1769,19 @@
17681769
17691770
**[⬆ Back to Top](#table-of-contents)**
17701771
1771-
55. ### How are error boundaries handled in React v15?
1772+
56. ### How are error boundaries handled in React v15?
17721773
17731774
React v15 provided very basic support for _error boundaries_ using `unstable_handleError` method. It has been renamed to `componentDidCatch` in React v16.
17741775
17751776
**[⬆ Back to Top](#table-of-contents)**
17761777
1777-
56. ### What are the recommended ways for static type checking?
1778+
57. ### What are the recommended ways for static type checking?
17781779
17791780
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.
17801781
17811782
**[⬆ Back to Top](#table-of-contents)**
17821783
1783-
57. ### What is the use of `react-dom` package?
1784+
58. ### What is the use of `react-dom` package?
17841785
17851786
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:
17861787
@@ -1792,7 +1793,7 @@
17921793
17931794
**[⬆ Back to Top](#table-of-contents)**
17941795
1795-
58. ### What is the purpose of render method of `react-dom`?
1796+
59. ### What is the purpose of render method of `react-dom`?
17961797
17971798
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.
17981799
@@ -1804,7 +1805,7 @@
18041805
18051806
**[⬆ Back to Top](#table-of-contents)**
18061807
1807-
59. ### What is ReactDOMServer?
1808+
60. ### What is ReactDOMServer?
18081809
18091810
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:
18101811
@@ -1831,7 +1832,7 @@
18311832
18321833
**[⬆ Back to Top](#table-of-contents)**
18331834
1834-
60. ### How to use innerHTML in React?
1835+
61. ### How to use innerHTML in React?
18351836
18361837
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.
18371838
@@ -1849,7 +1850,7 @@
18491850
18501851
**[⬆ Back to Top](#table-of-contents)**
18511852
1852-
61. ### How to use styles in React?
1853+
62. ### How to use styles in React?
18531854
18541855
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.
18551856
@@ -1868,7 +1869,7 @@
18681869
18691870
**[⬆ Back to Top](#table-of-contents)**
18701871
1871-
62. ### How events are different in React?
1872+
63. ### How events are different in React?
18721873
18731874
Handling events in React elements has some syntactic differences:
18741875
@@ -1877,13 +1878,13 @@
18771878
18781879
**[⬆ Back to Top](#table-of-contents)**
18791880
1880-
63. ### What will happen if you use `setState()` in constructor?
1881+
64. ### What will happen if you use `setState()` in constructor?
18811882
18821883
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.
18831884
18841885
**[⬆ Back to Top](#table-of-contents)**
18851886
1886-
64. ### What is the impact of indexes as keys?
1887+
65. ### What is the impact of indexes as keys?
18871888
18881889
Keys should be stable, predictable, and unique so that React can keep track of elements.
18891890
@@ -1905,7 +1906,7 @@
19051906
19061907
**[⬆ Back to Top](#table-of-contents)**
19071908
1908-
65. ### Is it good to use `setState()` in `componentWillMount()` method?
1909+
66. ### Is it good to use `setState()` in `componentWillMount()` method?
19091910
19101911
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()`.
19111912
@@ -1922,7 +1923,7 @@
19221923
19231924
**[⬆ Back to Top](#table-of-contents)**
19241925
1925-
66. ### What will happen if you use props in initial state?
1926+
67. ### What will happen if you use props in initial state?
19261927
19271928
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.
19281929
@@ -1965,7 +1966,7 @@
19651966
19661967
**[⬆ Back to Top](#table-of-contents)**
19671968
1968-
67. ### How do you conditionally render components?
1969+
68. ### How do you conditionally render components?
19691970
19701971
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.
19711972
@@ -1991,7 +1992,7 @@
19911992
19921993
**[⬆ Back to Top](#table-of-contents)**
19931994
1994-
68. ### Why we need to be careful when spreading props on DOM elements?
1995+
69. ### Why we need to be careful when spreading props on DOM elements?
19951996
19961997
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.
19971998
@@ -2009,7 +2010,7 @@
20092010
20102011
**[⬆ Back to Top](#table-of-contents)**
20112012
2012-
69. ### How you use decorators in React?
2013+
70. ### How you use decorators in React?
20132014
20142015
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.
20152016
@@ -2041,7 +2042,7 @@
20412042
20422043
**[⬆ Back to Top](#table-of-contents)**
20432044
2044-
70. ### How do you memoize a component?
2045+
71. ### How do you memoize a component?
20452046
20462047
There are memoize libraries available which can be used on function components.
20472048
@@ -2073,7 +2074,7 @@
20732074
20742075
**[⬆ Back to Top](#table-of-contents)**
20752076
2076-
71. ### How you implement Server Side Rendering or SSR?
2077+
72. ### How you implement Server Side Rendering or SSR?
20772078
20782079
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.
20792080
@@ -2088,13 +2089,13 @@
20882089
20892090
**[⬆ Back to Top](#table-of-contents)**
20902091
2091-
72. ### How to enable production mode in React?
2092+
73. ### How to enable production mode in React?
20922093
20932094
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.
20942095
20952096
**[⬆ Back to Top](#table-of-contents)**
20962097
2097-
73. ### What is CRA and its benefits?
2098+
74. ### What is CRA and its benefits?
20982099
20992100
The `create-react-app` CLI tool allows you to quickly create & run React applications with no configuration step.
21002101
@@ -2125,7 +2126,7 @@
21252126
21262127
**[⬆ Back to Top](#table-of-contents)**
21272128
2128-
74. ### What is the lifecycle methods order in mounting?
2129+
75. ### What is the lifecycle methods order in mounting?
21292130
21302131
The lifecycle methods are called in the following order when an instance of a component is being created and inserted into the DOM.
21312132
@@ -2136,7 +2137,7 @@
21362137

21372138
**[⬆ Back to Top](#table-of-contents)**
21382139

2139-
75. ### What are the lifecycle methods going to be deprecated in React v16?
2140+
76. ### What are the lifecycle methods going to be deprecated in React v16?
21402141

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

@@ -2148,7 +2149,7 @@
21482149

21492150
**[⬆ Back to Top](#table-of-contents)**
21502151

2151-
76. ### What is the purpose of `getDerivedStateFromProps()` lifecycle method?
2152+
77. ### What is the purpose of `getDerivedStateFromProps()` lifecycle method?
21522153

21532154
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.
21542155

@@ -2164,7 +2165,7 @@
21642165
21652166
**[⬆ Back to Top](#table-of-contents)**
21662167
2167-
77. ### What is the purpose of `getSnapshotBeforeUpdate()` lifecycle method?
2168+
78. ### What is the purpose of `getSnapshotBeforeUpdate()` lifecycle method?
21682169
21692170
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()`.
21702171
@@ -2180,13 +2181,13 @@
21802181
21812182
**[⬆ Back to Top](#table-of-contents)**
21822183
2183-
78. ### Do Hooks replace render props and higher order components?
2184+
79. ### Do Hooks replace render props and higher order components?
21842185
21852186
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.
21862187
21872188
**[⬆ Back to Top](#table-of-contents)**
21882189
2189-
79. ### What is the recommended way for naming components?
2190+
80. ### What is the recommended way for naming components?
21902191
21912192
It is recommended to name the component by reference instead of using `displayName`.
21922193
@@ -2218,7 +2219,7 @@
22182219
22192220
**[⬆ Back to Top](#table-of-contents)**
22202221
2221-
80. ### What is the recommended ordering of methods in component class?
2222+
81. ### What is the recommended ordering of methods in component class?
22222223
22232224
_Recommended_ ordering of methods from _mounting_ to _render stage_:
22242225
@@ -2239,7 +2240,7 @@
22392240

22402241
**[⬆ Back to Top](#table-of-contents)**
22412242

2242-
81. ### What is a switching component?
2243+
82. ### What is a switching component?
22432244

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

@@ -2272,7 +2273,7 @@
22722273
22732274
**[⬆ Back to Top](#table-of-contents)**
22742275
2275-
82. ### Why we need to pass a function to setState()?
2276+
83. ### Why we need to pass a function to setState()?
22762277
22772278
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()`.
22782279

0 commit comments

Comments
 (0)