|
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 |
|
|
1598 | 1609 |
|
1599 | 1610 | 48. ### What are portals in React?
|
1600 | 1611 |
|
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. |
1602 | 1614 |
|
1603 | 1615 | ```javascript
|
1604 | 1616 | ReactDOM.createPortal(child, container);
|
|
1608 | 1620 |
|
1609 | 1621 | **[⬆ Back to Top](#table-of-contents)**
|
1610 | 1622 |
|
1611 |
| -49. ### What are stateless components? |
| 1623 | +50. ### What are stateless components? |
1612 | 1624 |
|
1613 | 1625 | 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.
|
1614 | 1626 |
|
1615 | 1627 | **[⬆ Back to Top](#table-of-contents)**
|
1616 | 1628 |
|
1617 |
| -50. ### What are stateful components? |
| 1629 | +51. ### What are stateful components? |
1618 | 1630 |
|
1619 | 1631 | 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_.
|
1620 | 1632 |
|
|
1666 | 1678 |
|
1667 | 1679 | **[⬆ Back to Top](#table-of-contents)**
|
1668 | 1680 |
|
1669 |
| -51. ### How to apply validation on props in React? |
| 1681 | +52. ### How to apply validation on props in React? |
1670 | 1682 |
|
1671 | 1683 | 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`.
|
1672 | 1684 |
|
|
1731 | 1743 |
|
1732 | 1744 | **[⬆ Back to Top](#table-of-contents)**
|
1733 | 1745 |
|
1734 |
| -52. ### What are the advantages of React? |
| 1746 | +53. ### What are the advantages of React? |
1735 | 1747 |
|
1736 | 1748 | Below are the list of main advantages of React,
|
1737 | 1749 |
|
|
1743 | 1755 |
|
1744 | 1756 | **[⬆ Back to Top](#table-of-contents)**
|
1745 | 1757 |
|
1746 |
| -53. ### What are the limitations of React? |
| 1758 | +54. ### What are the limitations of React? |
1747 | 1759 |
|
1748 | 1760 | Apart from the advantages, there are few limitations of React too,
|
1749 | 1761 |
|
|
1755 | 1767 |
|
1756 | 1768 | **[⬆ Back to Top](#table-of-contents)**
|
1757 | 1769 |
|
1758 |
| -54. ### What are error boundaries in React v16? |
| 1770 | +55. ### What are error boundaries in React v16? |
1759 | 1771 |
|
1760 | 1772 | _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.
|
1761 | 1773 |
|
|
1798 | 1810 |
|
1799 | 1811 | **[⬆ Back to Top](#table-of-contents)**
|
1800 | 1812 |
|
1801 |
| -55. ### How are error boundaries handled in React v15? |
| 1813 | +56. ### How are error boundaries handled in React v15? |
1802 | 1814 |
|
1803 | 1815 | React v15 provided very basic support for _error boundaries_ using `unstable_handleError` method. It has been renamed to `componentDidCatch` in React v16.
|
1804 | 1816 |
|
1805 | 1817 | **[⬆ Back to Top](#table-of-contents)**
|
1806 | 1818 |
|
1807 |
| -56. ### What are the recommended ways for static type checking? |
| 1819 | +57. ### What are the recommended ways for static type checking? |
1808 | 1820 |
|
1809 | 1821 | 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.
|
1810 | 1822 |
|
1811 | 1823 | **[⬆ Back to Top](#table-of-contents)**
|
1812 | 1824 |
|
1813 |
| -57. ### What is the use of `react-dom` package? |
| 1825 | +58. ### What is the use of `react-dom` package? |
1814 | 1826 |
|
1815 | 1827 | 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:
|
1816 | 1828 |
|
|
1822 | 1834 |
|
1823 | 1835 | **[⬆ Back to Top](#table-of-contents)**
|
1824 | 1836 |
|
1825 |
| -58. ### What is the purpose of render method of `react-dom`? |
| 1837 | +59. ### What is the purpose of render method of `react-dom`? |
1826 | 1838 |
|
1827 | 1839 | 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.
|
1828 | 1840 |
|
|
1834 | 1846 |
|
1835 | 1847 | **[⬆ Back to Top](#table-of-contents)**
|
1836 | 1848 |
|
1837 |
| -59. ### What is ReactDOMServer? |
| 1849 | +60. ### What is ReactDOMServer? |
1838 | 1850 |
|
1839 | 1851 | 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:
|
1840 | 1852 |
|
|
1861 | 1873 |
|
1862 | 1874 | **[⬆ Back to Top](#table-of-contents)**
|
1863 | 1875 |
|
1864 |
| -60. ### How to use innerHTML in React? |
| 1876 | +61. ### How to use innerHTML in React? |
1865 | 1877 |
|
1866 | 1878 | 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.
|
1867 | 1879 |
|
|
1879 | 1891 |
|
1880 | 1892 | **[⬆ Back to Top](#table-of-contents)**
|
1881 | 1893 |
|
1882 |
| -61. ### How to use styles in React? |
| 1894 | +62. ### How to use styles in React? |
1883 | 1895 |
|
1884 | 1896 | 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.
|
1885 | 1897 |
|
|
1898 | 1910 |
|
1899 | 1911 | **[⬆ Back to Top](#table-of-contents)**
|
1900 | 1912 |
|
1901 |
| -62. ### How events are different in React? |
| 1913 | +63. ### How events are different in React? |
1902 | 1914 |
|
1903 | 1915 | Handling events in React elements has some syntactic differences:
|
1904 | 1916 |
|
|
1907 | 1919 |
|
1908 | 1920 | **[⬆ Back to Top](#table-of-contents)**
|
1909 | 1921 |
|
1910 |
| -63. ### What will happen if you use `setState()` in constructor? |
| 1922 | +64. ### What will happen if you use `setState()` in constructor? |
1911 | 1923 |
|
1912 | 1924 | 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.
|
1913 | 1925 |
|
1914 | 1926 | **[⬆ Back to Top](#table-of-contents)**
|
1915 | 1927 |
|
1916 |
| -64. ### What is the impact of indexes as keys? |
| 1928 | +65. ### What is the impact of indexes as keys? |
1917 | 1929 |
|
1918 | 1930 | Keys should be stable, predictable, and unique so that React can keep track of elements.
|
1919 | 1931 |
|
|
1936 | 1948 |
|
1937 | 1949 | **[⬆ Back to Top](#table-of-contents)**
|
1938 | 1950 |
|
1939 |
| -65. ### Is it good to use `setState()` in `componentWillMount()` method? |
| 1951 | +66. ### Is it good to use `setState()` in `componentWillMount()` method? |
1940 | 1952 |
|
1941 | 1953 | 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()`.
|
1942 | 1954 |
|
|
1953 | 1965 |
|
1954 | 1966 | **[⬆ Back to Top](#table-of-contents)**
|
1955 | 1967 |
|
1956 |
| -66. ### What will happen if you use props in initial state? |
| 1968 | +67. ### What will happen if you use props in initial state? |
1957 | 1969 |
|
1958 | 1970 | 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.
|
1959 | 1971 |
|
|
1996 | 2008 |
|
1997 | 2009 | **[⬆ Back to Top](#table-of-contents)**
|
1998 | 2010 |
|
1999 |
| -67. ### How do you conditionally render components? |
| 2011 | +68. ### How do you conditionally render components? |
2000 | 2012 |
|
2001 | 2013 | 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.
|
2002 | 2014 |
|
|
2022 | 2034 |
|
2023 | 2035 | **[⬆ Back to Top](#table-of-contents)**
|
2024 | 2036 |
|
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? |
2026 | 2038 |
|
2027 | 2039 | 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.
|
2028 | 2040 |
|
|
2040 | 2052 |
|
2041 | 2053 | **[⬆ Back to Top](#table-of-contents)**
|
2042 | 2054 |
|
2043 |
| -69. ### How you use decorators in React? |
| 2055 | +70. ### How you use decorators in React? |
2044 | 2056 |
|
2045 | 2057 | 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.
|
2046 | 2058 |
|
|
2072 | 2084 |
|
2073 | 2085 | **[⬆ Back to Top](#table-of-contents)**
|
2074 | 2086 |
|
2075 |
| -70. ### How do you memoize a component? |
| 2087 | +71. ### How do you memoize a component? |
2076 | 2088 |
|
2077 | 2089 | There are memoize libraries available which can be used on function components.
|
2078 | 2090 |
|
|
2104 | 2116 |
|
2105 | 2117 | **[⬆ Back to Top](#table-of-contents)**
|
2106 | 2118 |
|
2107 |
| -71. ### How you implement Server Side Rendering or SSR? |
| 2119 | +72. ### How you implement Server Side Rendering or SSR? |
2108 | 2120 |
|
2109 | 2121 | 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.
|
2110 | 2122 |
|
|
2119 | 2131 |
|
2120 | 2132 | **[⬆ Back to Top](#table-of-contents)**
|
2121 | 2133 |
|
2122 |
| -72. ### How to enable production mode in React? |
| 2134 | +73. ### How to enable production mode in React? |
2123 | 2135 |
|
2124 | 2136 | 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.
|
2125 | 2137 |
|
2126 | 2138 | **[⬆ Back to Top](#table-of-contents)**
|
2127 | 2139 |
|
2128 |
| -73. ### What is CRA and its benefits? |
| 2140 | +74. ### What is CRA and its benefits? |
2129 | 2141 |
|
2130 | 2142 | The `create-react-app` CLI tool allows you to quickly create & run React applications with no configuration step.
|
2131 | 2143 |
|
|
2156 | 2168 |
|
2157 | 2169 | **[⬆ Back to Top](#table-of-contents)**
|
2158 | 2170 |
|
2159 |
| -74. ### What is the lifecycle methods order in mounting? |
| 2171 | +75. ### What is the lifecycle methods order in mounting? |
2160 | 2172 |
|
2161 | 2173 | The lifecycle methods are called in the following order when an instance of a component is being created and inserted into the DOM.
|
2162 | 2174 |
|
|
2167 | 2179 |
|
2168 | 2180 | **[⬆ Back to Top](#table-of-contents)**
|
2169 | 2181 |
|
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? |
2171 | 2183 |
|
2172 | 2184 | The following lifecycle methods going to be unsafe coding practices and will be more problematic with async rendering.
|
2173 | 2185 |
|
|
2179 | 2191 |
|
2180 | 2192 | **[⬆ Back to Top](#table-of-contents)**
|
2181 | 2193 |
|
2182 |
| -76. ### What is the purpose of `getDerivedStateFromProps()` lifecycle method? |
| 2194 | +77. ### What is the purpose of `getDerivedStateFromProps()` lifecycle method? |
2183 | 2195 |
|
2184 | 2196 | 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.
|
2185 | 2197 |
|
|
2195 | 2207 |
|
2196 | 2208 | **[⬆ Back to Top](#table-of-contents)**
|
2197 | 2209 |
|
2198 |
| -77. ### What is the purpose of `getSnapshotBeforeUpdate()` lifecycle method? |
| 2210 | +78. ### What is the purpose of `getSnapshotBeforeUpdate()` lifecycle method? |
2199 | 2211 |
|
2200 | 2212 | 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()`.
|
2201 | 2213 |
|
|
2211 | 2223 |
|
2212 | 2224 | **[⬆ Back to Top](#table-of-contents)**
|
2213 | 2225 |
|
2214 |
| -78. ### Do Hooks replace render props and higher order components? |
| 2226 | +79. ### Do Hooks replace render props and higher order components? |
2215 | 2227 |
|
2216 | 2228 | 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.
|
2217 | 2229 |
|
2218 | 2230 | **[⬆ Back to Top](#table-of-contents)**
|
2219 | 2231 |
|
2220 |
| -79. ### What is the recommended way for naming components? |
| 2232 | +80. ### What is the recommended way for naming components? |
2221 | 2233 |
|
2222 | 2234 | It is recommended to name the component by reference instead of using `displayName`.
|
2223 | 2235 |
|
|
2249 | 2261 |
|
2250 | 2262 | **[⬆ Back to Top](#table-of-contents)**
|
2251 | 2263 |
|
2252 |
| -80. ### What is the recommended ordering of methods in component class? |
| 2264 | +81. ### What is the recommended ordering of methods in component class? |
2253 | 2265 |
|
2254 | 2266 | _Recommended_ ordering of methods from _mounting_ to _render stage_:
|
2255 | 2267 |
|
|
2270 | 2282 |
|
2271 | 2283 | **[⬆ Back to Top](#table-of-contents)**
|
2272 | 2284 |
|
2273 |
| -81. ### What is a switching component? |
| 2285 | +82. ### What is a switching component? |
2274 | 2286 |
|
2275 | 2287 | A _switching component_ is a component that renders one of many components. We need to use object to map prop values to components.
|
2276 | 2288 |
|
|
2303 | 2315 |
|
2304 | 2316 | **[⬆ Back to Top](#table-of-contents)**
|
2305 | 2317 |
|
2306 |
| -82. ### Why we need to pass a function to setState()? |
| 2318 | +83. ### Why we need to pass a function to setState()? |
2307 | 2319 |
|
2308 | 2320 | 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()`.
|
2309 | 2321 |
|
|
0 commit comments