React Flashcards
What is React?
- React is a library for making user interfaces through JavaScript
- Frameworks are types of libraries (React is a library)
What is a React element?
React elements are objects that describe what will be shown. React DOM takes care of manipulating the DOM with the provided React elements. They are not DOM objects
How do you mount a React element to the DOM?
- Query for/create a DOM element to create the React root with
- Attach the root to that DOM object using
const root = ReactDOM.createRoot($container)
- Render React elements on that root using
root.render(myElement)
- Removes everything in the DOM element and replaces it with the React elements
What’s the difference between a library and a framework?
- Frameworks have inversion of control (who calls who)
- Frameworks generally have events
- You define things and hand them off
- Express is a framework: You define things like
app.get
andapp.post
, but something else handles when they get called - Lodash is a library: You get a bunch of functions from Lodash and use them when you want
- Express is a framework: You define things like
- Library is a superset with frameworks in it
What is Babel?
Babel is a toolchain that can analyze JavaScript code that is written with newer standards and convert it into backwards-compatible JavaScript.
A JavaScript compiler
What is a Plug-in?
A plugin enhances a program’s capabilities
What is a Webpack loader?
A Webpack loader is a step/operation that acts on source code, allowing the code to be transformed/processed as Webpack bundles the code together
How can you make Babel and Webpack work together?
The babel-loader
should be specified in the webpack.config.js
file under module.rules
Additionally, babel-loader
should be npm install
ed along with any Babel plugins such as @babel/plugin-transform-arrow-functions
(don’t forget @babel/core
as well)
Plugins to be used should be specified in the module.rules
as well
What is JSX?
- JSX is a syntax extension of JavaScript, and it gets converted to normal JavaScript by a module bundler like Webpack using something like Babel with the appropriate plugin
- Typically used with React
- Structuring UI is similar to writing HTML
Why must the React object be imported when authoring JSX in a module?
The React object has the methods necessary to actually create the React elements (the objects). Those methods are called after the JSX syntax is converted into normal JavaScript.
How can you make Webpack and Babel work together to convert JSX into valid JavaScript?
- Install
babel-loader
,@babel/core
, and the plugin@babel/plugin-transform-react-jsx
using NPM (don’t forgetwebpack
andbabel
in general) - Set the loader to run with the plugin in the
webpack.config.js
file - When Webpack starts bundling from
npm run build
, it will process.jsx
files using the plugin
What is a React component?
It’s a function that outputs a React element
How do you define a function component in React?
-
function CapitalName(props) { return Stuff {props.something} Here }
- Name of the function component must start with a capital
- Has one parameter
props
-
return
s a React element - Arguments are accessed with curly braces containing an expression, usually accessing the
props
object
How do you mount a component to the DOM?
- Query for the root DOM element
- Use
ReactDOM.createRoot($DOMroot)
to create the React DOM root - With the React DOM root, call
.render()
with the desired React elements created from the React component inside
- Use
What are props in React?
Props are arbitrary inputs passed to a React component (props
is an object)
How do you pass props to a component?
- When creating an element from a component, enter them after the component name like
or if you have an expression as a prop, wrap the expression in curly braces like
- Objects, variables, anything other than a string must be in curly braces
How do you write JavaScript expressions in JSX?
Wrap them in curly braces, otherwise they’re the same as JavaScript expressions
How do you create “class” component in React?
- You create a new class that inherits from the
React.Component
prototype- You can use the ES6
extend
syntax likeclass MyClass extends React.Component {}
- The class must have at least a
render
method defined (which must return a React element)
- You can use the ES6
How do you access props in a class component?
The props
object will be a property of this
, so use this.props
to access any props
What is the purpose of state in React?
State allows a React element to not only handle events but also keep the functionality of handling events within the component
How to you pass an event handler to a React element?
Within the return of the React element (the render()
), add the event as a prop to the React element and specify the handler function as the value to that prop but remember to enclose it within curly braces and specify that it is a method of this
What are controlled components?
Controlled components are React components that render the form control and handle the state of the form control with the React state
as the single source of truth
What two props must you pass to an input for it to be “controlled”?
-
value
with the associated key value inthis.state
for that input- An event handler for
onChange
, and this event handler should be usingsetState()
in order to manage the state of the component
- An event handler for
WhatArray
method is commonly used to create a list of React elements?
myArray.map()
because it returns an array of the same length as myArray
, one for each element
What is the best value to use as a “key” prop when rendering lists?
- An ideal key would be one that uniquely identifies a list item from its siblings, from the source data
- Indices are not preferred in the event that the order of the items may change, but can be used
When does React call a component’scomponentDidMount
method?
It is called immediately after the component is inserted into the tree
- After first successful render
Name three React.Component lifecycle methods. (This is important for React jobs!)
-
componentDidMount()
componentDidUpdate()
componentWillUnmount()
render()
constructor()
How do you pass data to a child component?
The parent can hold the data in its state, and pass that data as a prop to the child component when creating the child element