└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # React recaps v18.2.0 2 | Recaps from react documentation https://react.dev/ 3 | --- 4 | 5 |
6 | 7 | ## Describing the UI 8 | 9 | ### Your First Component 10 | 11 | You’ve just gotten your first taste of React! Let’s recap some key points. 12 | - React lets you create components, **reusable UI elements for your app.** 13 | 14 | - In a React app, every piece of UI is a component. 15 | 16 | - React components are regular JavaScript functions except: 17 | 18 | 1. Their names always begin with a capital letter. 19 | 2. They return JSX markup. 20 | 21 | *link: https://react.dev/learn/your-first-component* 22 | 23 |
24 | 25 | ### Importing and Exporting Components 26 | 27 | On this page you learned: 28 | 29 | - What a root component file is 30 | - How to import and export a component 31 | - When and how to use default and named imports and exports 32 | - How to export multiple components from the same file 33 | 34 | *link: https://react.dev/learn/importing-and-exporting-components* 35 | 36 |
37 | 38 | 39 | ### Writing Markup with JSX 40 | 41 | Now you know why JSX exists and how to use it in components: 42 | 43 | - React components group rendering logic together with markup because they are related. 44 | - JSX is similar to HTML, with a few differences. You can use a [converter](https://transform.tools/html-to-jsx) if you need to. 45 | - Error messages will often point you in the right direction to fixing your markup. 46 | 47 | *link: https://react.dev/learn/writing-markup-with-jsx* 48 | 49 |
50 | 51 | 52 | ### JavaScript in JSX with Curly Braces 53 | 54 | Now you know almost everything about JSX: 55 | 56 | - JSX attributes inside quotes are passed as strings. 57 | - Curly braces let you bring JavaScript logic and variables into your markup. 58 | - They work inside the JSX tag content or immediately after `=` in attributes. 59 | - `{{` and `}}` is not special syntax: it’s a JavaScript object tucked inside JSX curly braces. 60 | 61 | *link: https://react.dev/learn/javascript-in-jsx-with-curly-braces* 62 | 63 |
64 | 65 | 66 | ### Passing Props to a Component 67 | 68 | - To pass props, add them to the JSX, just like you would with HTML attributes. 69 | - To read props, use the `function Avatar({ person, size })` destructuring syntax. 70 | - You can specify a default value like `size = 100`, which is used for missing and `undefined` props. 71 | - You can forward all props with `` JSX spread syntax, but don’t overuse it! 72 | - Nested JSX like `` will appear as `Card` component’s `children` prop. 73 | - Props are read-only snapshots in time: every render receives a new version of props. 74 | - You can’t change props. When you need interactivity, you’ll need to set state. 75 | 76 | *link: https://react.dev/learn/passing-props-to-a-component* 77 | 78 |
79 | 80 | 81 | ### Conditional Rendering 82 | 83 | - In React, you control branching logic with JavaScript. 84 | - You can return a JSX expression conditionally with an `if` statement. 85 | - You can conditionally save some JSX to a variable and then include it inside other JSX by using the curly braces. 86 | - In JSX, `{cond ? : }` means _“if `cond`, render ``, otherwise ``”_. 87 | - In JSX, `{cond && }` means _“if `cond`, render ``, otherwise nothing”_. 88 | - The shortcuts are common, but you don’t have to use them if you prefer plain `if`. 89 | 90 | *link: https://react.dev/learn/conditional-rendering* 91 | 92 |
93 | 94 | 95 | ### Rendering Lists 96 | 97 | On this page you learned: 98 | 99 | - How to move data out of components and into data structures like arrays and objects. 100 | - How to generate sets of similar components with JavaScript’s `map()`. 101 | - How to create arrays of filtered items with JavaScript’s `filter()`. 102 | - Why and how to set `key` on each component in a collection so React can keep track of each of them even if their position or data changes. 103 | 104 | *link: https://react.dev/learn/rendering-lists* 105 | 106 |
107 | 108 | 109 | ### Keeping Components Pure 110 | 111 | - A component must be pure, meaning: 112 | - **It minds its own business.** It should not change any objects or variables that existed before rendering. 113 | - **Same inputs, same output.** Given the same inputs, a component should always return the same JSX. 114 | - Rendering can happen at any time, so components should not depend on each others’ rendering sequence. 115 | - You should not mutate any of the inputs that your components use for rendering. That includes props, state, and context. To update the screen, [“set” state](https://react.dev/learn/state-a-components-memory) instead of mutating preexisting objects. 116 | - Strive to express your component’s logic in the JSX you return. When you need to “change things”, you’ll usually want to do it in an event handler. As a last resort, you can `useEffect`. 117 | - Writing pure functions takes a bit of practice, but it unlocks the power of React’s paradigm. 118 | 119 | *link: https://react.dev/learn/keeping-components-pure* 120 | 121 | --- 122 | 123 |
124 | 125 | ## Adding Interactivity 126 | 127 | 128 | ### Responding to Events 129 | 130 | - You can handle events by passing a function as a prop to an element like `