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)