|
978 | 978 | 2. If you extract list item as separate component then apply _keys_ on list component instead of `li` tag.
|
979 | 979 | 3. There will be a warning message in the console if the `key` prop is not present on list items.
|
980 | 980 | 4. The key attribute accepts either string or number and internally convert it as string type.
|
| 981 | + 5. Don't generate the key on the fly something like `key={Math.random()}`. Because the keys will never match up between re-renders and DOM created everytime. |
981 | 982 |
|
982 | 983 | **[⬆ Back to Top](#table-of-contents)**
|
983 | 984 |
|
|
1354 | 1355 | {children}
|
1355 | 1356 | </div>;
|
1356 | 1357 | );
|
1357 |
| - }; |
| 1358 | + } |
1358 | 1359 |
|
1359 | 1360 | export default function Greeting() {
|
1360 | 1361 | return (
|
|
1916 | 1917 |
|
1917 | 1918 | Keys should be stable, predictable, and unique so that React can keep track of elements.
|
1918 | 1919 |
|
1919 |
| - In the below code snippet each element's key will be based on ordering, rather than tied to the data that is being represented. This limits the optimizations that React can do. |
| 1920 | + In the below code snippet each element's key will be based on ordering, rather than tied to the data that is being represented. This limits the optimizations that React can do and creates confusing bugs in the application. |
1920 | 1921 |
|
1921 | 1922 | ```jsx harmony
|
1922 | 1923 | {
|
1923 | 1924 | todos.map((todo, index) => <Todo {...todo} key={index} />);
|
1924 | 1925 | }
|
1925 | 1926 | ```
|
1926 | 1927 |
|
1927 |
| - If you use element data for unique key, assuming todo.id is unique to this list and stable, React would be able to reorder elements without needing to reevaluate them as much. |
| 1928 | + If you use element data for unique key, assuming `todo.id` is unique to this list and stable, React would be able to reorder elements without needing to reevaluate them as much. |
1928 | 1929 |
|
1929 | 1930 | ```jsx harmony
|
1930 | 1931 | {
|
1931 | 1932 | todos.map((todo) => <Todo {...todo} key={todo.id} />);
|
1932 | 1933 | }
|
1933 | 1934 | ```
|
| 1935 | + **Note:** If you don't specify `key` prop at all, React will use index as a key's value while iterating over an array of data. |
1934 | 1936 |
|
1935 | 1937 | **[⬆ Back to Top](#table-of-contents)**
|
1936 | 1938 |
|
@@ -7272,30 +7274,30 @@ const loadUser = async () => {
|
7272 | 7274 |
|
7273 | 7275 | 336. ### What is strict mode in React?
|
7274 | 7276 |
|
7275 |
| - `React.StrictMode` is a useful component for highlighting potential problems in an application. Just like `<Fragment>`, `<StrictMode>` does not render any extra DOM elements. It activates additional checks and warnings for its descendants. These checks apply for _development mode_ only. |
| 7277 | + `React.StrictMode` is a useful component for highlighting potential problems in an application. Just like `<Fragment>`, `<StrictMode>` does not render any extra DOM elements. It activates additional checks and warnings for its descendants. These checks apply for _development mode_ only. |
7276 | 7278 |
|
7277 |
| - ```jsx harmony |
7278 |
| - import React from "react"; |
| 7279 | + ```jsx harmony |
| 7280 | + import React from "react"; |
7279 | 7281 |
|
7280 |
| - function ExampleApplication() { |
7281 |
| - return ( |
7282 |
| - <div> |
7283 |
| - <Header /> |
7284 |
| - <React.StrictMode> |
7285 |
| - <div> |
7286 |
| - <ComponentOne /> |
7287 |
| - <ComponentTwo /> |
7288 |
| - </div> |
7289 |
| - </React.StrictMode> |
7290 |
| - <Header /> |
7291 |
| - </div> |
7292 |
| - ); |
7293 |
| - } |
7294 |
| - ``` |
| 7282 | + function ExampleApplication() { |
| 7283 | + return ( |
| 7284 | + <div> |
| 7285 | + <Header /> |
| 7286 | + <React.StrictMode> |
| 7287 | + <div> |
| 7288 | + <ComponentOne /> |
| 7289 | + <ComponentTwo /> |
| 7290 | + </div> |
| 7291 | + </React.StrictMode> |
| 7292 | + <Header /> |
| 7293 | + </div> |
| 7294 | + ); |
| 7295 | + } |
| 7296 | + ``` |
7295 | 7297 |
|
7296 |
| - In the example above, the _strict mode_ checks apply to `<ComponentOne>` and `<ComponentTwo>` components only. i.e., Part of the application only. |
| 7298 | + In the example above, the _strict mode_ checks apply to `<ComponentOne>` and `<ComponentTwo>` components only. i.e., Part of the application only. |
7297 | 7299 |
|
7298 |
| - **[⬆ Back to Top](#table-of-contents)** |
| 7300 | + **[⬆ Back to Top](#table-of-contents)** |
7299 | 7301 |
|
7300 | 7302 | 337. ### What is the benefit of strict mode?
|
7301 | 7303 |
|
@@ -7341,16 +7343,16 @@ const loadUser = async () => {
|
7341 | 7343 | **[⬆ Back to Top](#table-of-contents)**
|
7342 | 7344 |
|
7343 | 7345 | 339. ### What are the rules of JSX?
|
7344 |
| - The below 3 rules needs to be followed while using JSX in a react application. |
| 7346 | + The below 3 rules needs to be followed while using JSX in a react application. |
7345 | 7347 |
|
7346 |
| - 1. **Return a single root element**: |
| 7348 | + 1. **Return a single root element**: |
7347 | 7349 | If you are returning multiple elements from a component, wrap them in a single parent element. Otherwise you will receive the below error in your browser console.
|
7348 | 7350 |
|
7349 | 7351 | ```html Adjacent JSX elements must be wrapped in an enclosing tag.```
|
7350 | 7352 |
|
7351 |
| - 2. **All the tags needs to be closed:** |
| 7353 | + 2. **All the tags needs to be closed:** |
7352 | 7354 | Unlike HTML, all tags needs to closed explicitly with in JSX. This rule applies for self-closing tags(like hr, br and img tags) as well.
|
7353 |
| - 3. **Use camelCase naming:** |
| 7355 | + 3. **Use camelCase naming:** |
7354 | 7356 | It is suggested to use camelCase naming for attributes in JSX. For example, the common attributes of HTML elements such as `class`, `tabindex` will be used as `className` and `tabIndex`.
|
7355 | 7357 | **Note:** There is an exception for aria-* and data-* attributes which should be lower cased all the time.
|
7356 | 7358 |
|
|
0 commit comments