Main Concepts Flashcards
(248 cards)
What is the smallest React example?
ReactDOM.render(
< h1 >Hello, world!< /h1 >,
document.getElementById(‘root’)
);
What is this an example of?
const element = < h1 >Hello, world!< /h1 >;
JSX.
What is React?
A JavaScript library for building user interfaces.
What is JSX?
A syntax extension to JavaScript.
How is JSX not like a template language?
Because it comes with the full power of JavaScript.
What does JSX produce?
React elements.
What does React embrace?
The fact that rendering logic is inherently coupled with other UI logic: how events are handled, how the state changes over time, and how the data is prepared for display.
What would React do instead of artificially separating technologies by putting markup and logic in separate files?
Separates concerns with loosely coupled units called “components” that contain both.
Does React require JSX?
No.
How do people find JSX useful?
As a visual aid when working with UI inside the JavaScript code.
What does JSX allow React to do?
To show more useful error and warning messages.
Declare a variable called name and then use it inside JSX by wrapping it in curly braces.
const name = 'Josh Perez'; const element = < h1 >Hello, { name }< /h1 >;
ReactDOM.render(
element,
document.getElementById(‘root’)
);
What can you put inside the curly braces in JSX?
Any valid JavaScript expression.
How can we make JSX more readable?
By splitting it over multiple lines.
What should you also do when splitting JSX over multiple lines and why?
Wrapping it in parentheses to avoid the pitfalls of automatic semicolon insertion.
What happens to JSX after compilation?
They become regular JavaScript function calls and evaluate to JavaScript objects.
What does JSX being compiled mean?
You can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions.
Show JSX being used inside an if statement.
function getGreeting(user) { if (user) { return < h1 >Hello, { formatName(user) }!< /h1 >; } return < h1 >Hello, Stranger.< /h1 >; }
How would you specify string literals as attributes?
By using quotes.
Show an example of using quotes to specify string literals as attributes.
const element = < div tabIndex=”0” >< /div >;
How would you embed a JavaScript expression in an attribute?
By using curly braces.
Show an example of using curly braces to embed a JavaScript expression in an attribute.
const element = < img src={ user.avatarUrl } >< /img >;
What should you note about using quotes (for string values) or curly braces (for expressions) in attributes.
You should use one or the other, not both at the same time.
What naming convention is used for JSX?
camelCase.