React Flashcards
What are the 3 rules of JSX?
- Return a single root element.
- Close all the tags, including self-closing tags (< />).
- camelCase most things.
Why do multiple JSX tags need to be wrapped with a parent element?
JSX is JavaScript - even though it looks like HTML - and a function in JavaScript cannot return two objects without wrapping them into an array.
What is a React Fragment?
A special React element that can be used to wrap multiple JSX elements without actually adding an extra element into the DOM.
Why are JSX attributes written in camelCase?
Because they are converted into keys of JavaScript objects and those keys/variables cannot contain dashes (so ‘stroke-width’ becomes ‘strokeWidth’).
Why is the attribute ‘className’ used in React?
Because ‘class’ is a reserved JavaScript keyword.
Why do you need to compile your React code?
Because React used JSX which needs to be compiled into plain JavaScript.
What is a React component?
JavaScript functions that accept input - called ‘props’ - and return UI/HTML/JSX elements.
What is the naming convention for React components?
Components should start with a capital letter to distinguish them from plain HTML and JavaScript.
What are props?
Data passed to components as custom arguments/attributes that change the component’s behavior, look or what is rendered.
What data type are props?
Object
How are JavaScript variables used inside JSX markup?
By using the curly braces ‘{ }’ syntax.
What are the 4 JavaScript expressions that can be added inside JSX curly braces ‘{ }’ ?
- Object property with dot notation
<h1>{props.title}</h1> - Template literal
<h1>{Cool ${title}
}</h1> - Returned value of a function
<h1>{Cool ${title}
}</h1> - Ternary operator
<h1>{title ? title : ‘Default Title’}</h1>
What prop should each child in a list have?
A “key” prop
Why are “key” props necessary for all list items in React?
Because they uniquely identify the items in an array so that React knows what elements to update in the array.
What are props used for?
To pass information to components