Lesson 2: Rendering UI with React Flashcards
What will myBio hold when the following code is run?
import React from ‘react’;
const myBio = React.createElement(‘div’, null, ‘My name is Tyler, and I love porcupines.’);
A Javascript object
What’s the difference between .createElement() and .render() and how do they work together?
When we call .createElement() we haven’t created a real DOM element yet. It’s not until we call .render() that the browser actually creates a real DOM element.
Which of the following HTML attributes are allowed to be passed to the React element?
- poster
- id
- marginWidth
- for
poster, id and marginWidth
An interesting thing to note is that you can’t use the default for attribute. Just like you have to use className instead of class, you have to use htmlFor instead of for. This is because ‘for’ is a reserved word in JavaScript.
Describe the parameters of React.createElement
- type – either a string or a React Component
This can be a string of any existing HTML element (e.g. ‘p’, ‘span’, or ‘header’) or you could pass a React component (we’ll be creating components with JSX, in just a moment).
- props – either null or an object
This is an object of HTML attributes and custom data about the element.
- content – null, a string, a React Element, or a React Component
Anything that you pass here will be the content of the rendered element. This can include plain text, JavaScript code, other React elements, etc.
If your .createElement content argument contains multiple child elements, from an array or iterator for example, what is required for each child?
A unique key prop
How many React elements does .createElement() return?
It only returns one root element. You may nest more elements inside this root element, but you cannot have more than one element at the root level.
What is the single responsibility principle and why is it important?
It’s pretty simple: your component classes should just “do one thing”. This helps maintain the modularity and reusability of your components. If it manages too many different tasks, it may be a good idea to decompose your component into smaller subcomponents.
What do the following libraries from react-scripts do?
- Babel
- Webpack
- Webpack dev server
- Babel - transpiler allowing us to use latest ES6 syntax and JSX
- Webpack - generates the build
- Webpack dev server - gives us auto-reload behavior
What do the following yarn commands do?
- yarn start
- yarn build
- yarn test
- yarn eject
- yarn start - starts dev server
- yarn build - bundles app into static files for production
- yarn test - starts the test runner
- yarn eject - removes this tool and copies build dependencies, configuration files and scripts into the app directory. If you do this, you can’t go back!