React Cheatsheet Sintaxis Flashcards
Comprehensive React cheatsheet for beginners.
JSX Element
let element =
<h1>Hello, world!</h1>
;
let emptyHeading = <h1 />;
JSX Expressions
let name = ‘Josh Perez’;
let element = <h1> Hello, {name} </h1>;
function fullName (firstName, lastName) {
return firstName + ‘ ‘ +
lastName;
}
let element = <h1>Hello, {fullName(‘Julie’, ‘Johnson’)}</h1>
JSX attributes
const element =
<img src={user.avatarUrl} />;
const element =
<button>
Click me
</button>;
JSX Functions
name() {
return “Julie”;
}
return (
<h1>
Hi {name()}!
</h1>
)
Embed an external component
import React from ‘react’;
import ComponentName from ‘component-name’;
export default function UserProfile() {
return (
<div
className="UserProfile">
<ComponentName></ComponentName>
</div>
);
}
React Element Styles inline
<h1 style={{ fontSize: 24, margin: ‘0 auto’, textAlign: ‘center’ }}>
My header
</h1>
React Props
function App() {
return <User></User>
}
function User(props) {
return <h1>Hello, {props.name}</h1>; // Hello, John Doe!
}
React Props, extract the value of the “name”
function App() {
return <User></User>
}
function User({ name }) {
return <h1>Hello, {name}!</h1>; // Hello, John Doe!
}
React Children Props
function App() {
return (
<User>
<h1>Hello, John Doe!</h1>
</User>
);
}
function User({ children }) {
return children; // Hello, John Doe!
}
React IF Conditional
function App() {
const isAuthUser = useAuth();
if (isAuthUser) {
// if our user is authenticated, let them use the app
return <AuthApp></AuthApp>;
}
// if user is not authenticated, show a different screen
return <UnAuthApp></UnAuthApp>;
}
React ternary operator conditional
function App() {
const isAuthUser = useAuth();
return (
<>
<h1>My App</h1>
{isAuthUser ? <AuthApp></AuthApp> : <UnAuthApp></UnAuthApp>}
</>
)
}