2116024, 1-03. AM Lear React React Components Cheatsheet | Codecedemy
Cheatsheets / Learn React [codelcademy
React Components
return statement
React function components must contain a return f 1 MyComponent ()
statement. This should return some React elements
‘ereated with JS.
Function Component Base
React function components follow regular JavaScript f ,
‘lass syntax in declaration and returns JSX elements.
This example shows a simple React function component,
Importing React
In order to use React, we must frst import the React, sor .
library. When we import the library it creates an object
that contains properties needed to make React work,
including JSX and creating custom components.
hips www. codecademy comearnreact-101/modulesilearn-react-componentsicheatsheet 152116024, 1-03. AM
React Components
‘A React component isa reusable piece of code used to
define the appearance, behavior, and state of a portion of
1 web app's interface. Components are defined as
functions. Using the component as a factory, an infinite
number of component instances can be created.
JSX Capitalization
React requires thatthe first letter of components be
capitalized. JSX will use this capitalization to tell the
ifference between an HTML tag and a component
instance. Ifthe first letter of a name is capitalized, then
SK knows its @ component instance; if not, then i's an
HTML element.
Leam Reset:
React Components Chestsheet | Codecademy
function MyFunctionComp
// This is considered a
TML tag.
hitnsww.codecademy/comearnreact-101/modulesilearn-react-componentsicheatsheet
2182116024, 1-03. AM
Importing and Exporting Components in React
React components can be modulaly structured and
made reusable by placing them into their own fles.
Components can be exported and imported into a top=
level fle and rendered
In Appi
Rendering a Component
‘A React function component can be rendered by creating
1 root container and rendering the component into the
root container.
Lear React React Components Cheatsheet | Codecedemy
[codelcademy
Component to be rendered
function MyComponent ()
//Rendering the component
createRoot (
getElement! .
hitnsww.codecademy/comearnreact-101/modulesilearn-react-componentsicheatsheet 382116024, 1-03. AM Leam Reset:
Multi-line JSX Expressions
Parentheses are used when writing @ multi-line JSX
‘expression. In the example, we see that the component's
Tetum statement is spit over multiple Ines, Therefore it
is wrapped in parentheses.
Logic Before return
‘AReact component can contain JavaScript before any
ISKis returned. The JavaScript before the return
statement informs any logic necessary to render
‘component,
Inthe example code, we see JavaScript prior to the
return statement which rounds the value to an
integer.
React Components Chestsheet | Codecademy
[codelcademy
ank"
https: //en.wikipedia.ozg/wiki/Mahatm
gandhi"
round (
hitnsww.codecademy/comearnreact-101/modulesilearn-react-componentsicheatsheet
482116024, 1-03. AM Lear React React Components Cheatsheet | Codecedemy
[codelcademy
Object Properties As Attribute Values
In React, SX attribute values can be set through data .
stored in regular JavaSeript objects. We see this in the
‘example block of code.
In our code example we first see our JavaScript object
seaAnemones and the values stored with this image. ‘https: //commons .wikimedia.o:
We then see how these stored values are used to set the
attributes in our JSX expression for the
SeaAnemones component.
Jwiki/Catege
0429.
Images# /media/Pile:Anemon
px,
cL Print 8 Share v
hitnsww.codecademy/comearnreact-101/modulesilearn-react-componentsicheatsheet