React Flashcards

1
Q

What is React?

A
  • React is a library for making user interfaces through JavaScript
    • Frameworks are types of libraries (React is a library)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a React element?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you mount a React element to the DOM?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the difference between a library and a framework?

A
  • 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 and app.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
  • Library is a superset with frameworks in it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Babel?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Plug-in?

A

A plugin enhances a program’s capabilities

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Webpack loader?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How can you make Babel and Webpack work together?

A

The babel-loader should be specified in the webpack.config.js file under module.rules
Additionally, babel-loader should be npm installed 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is JSX?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why must the React object be imported when authoring JSX in a module?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you make Webpack and Babel work together to convert JSX into valid JavaScript?

A
  • Install babel-loader, @babel/core, and the plugin @babel/plugin-transform-react-jsx using NPM (don’t forget webpack and babel 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a React component?

A

It’s a function that outputs a React element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you define a function component in React?

A
  • function CapitalName(props) { return Stuff {props.something} Here }
    • Name of the function component must start with a capital
    • Has one parameter props
    • returns a React element
    • Arguments are accessed with curly braces containing an expression, usually accessing the props object
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you mount a component to the DOM?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are props in React?

A

Props are arbitrary inputs passed to a React component (props is an object)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you pass props to a component?

A
  • 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
17
Q

How do you write JavaScript expressions in JSX?

A

Wrap them in curly braces, otherwise they’re the same as JavaScript expressions

18
Q

How do you create “class” component in React?

A
  • You create a new class that inherits from the React.Component prototype
    • You can use the ES6 extend syntax like class MyClass extends React.Component {}
    • The class must have at least a render method defined (which must return a React element)
19
Q

How do you access props in a class component?

A

The props object will be a property of this, so use this.props to access any props

20
Q

What is the purpose of state in React?

A

State allows a React element to not only handle events but also keep the functionality of handling events within the component

21
Q

How to you pass an event handler to a React element?

A

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

22
Q

What are controlled components?

A

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

23
Q

What two props must you pass to an input for it to be “controlled”?

A
  • value with the associated key value in this.state for that input
    • An event handler for onChange, and this event handler should be using setState() in order to manage the state of the component
24
Q

WhatArraymethod is commonly used to create a list of React elements?

A

myArray.map() because it returns an array of the same length as myArray, one for each element

25
Q

What is the best value to use as a “key” prop when rendering lists?

A
  • 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
26
Q

When does React call a component’scomponentDidMountmethod?

A

It is called immediately after the component is inserted into the tree
- After first successful render

27
Q

Name three React.Component lifecycle methods. (This is important for React jobs!)

A
  • componentDidMount()
    • componentDidUpdate()
    • componentWillUnmount()
    • render()
    • constructor()
28
Q

How do you pass data to a child component?

A

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