React Flashcards

1
Q

What is Array.prototype.filter useful for?

A

It “filters” out the elements you don’t want and returns a new array of the elements you do want
It passes a function as the second argument to filter it out

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

What is Array.prototype.map useful for?

A

It applies some kind of function to every element in the array and returns a NEW array with those values

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

What is Array.prototype.reduce useful for?

A

It’s helpful for getting 1 result from all the elements of 1 array
The result, however, will move from ([0], [1]) the result of which is then used for the next function with the next one in line, [2], and then that result for the next one [3], and so on and so forth

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

What is Webpack?

A

Starts from multiple modules and packages and the end results is fewer files, common case is to make 1 bundle/file
Let’s us write modules but also support any module format and handle resources and assets at the same time
It’s a tool that lets you bundle your JS applications (supporting ESM and CommonJS) and it can be extended to support many different assets like images, fonts and stylesheets

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

How do you add a devDependency to a package?

A

Npm install [name like webpack or webpack-cli, or both] –save-dev

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

What is an NPM script?

A

Command line command with a nickname
The “scripts” property in the package.json file supports a number of built-in scripts and their preset life cycle events

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

How do you execute Webpack with npm run?

A

Add the nickname to the script property with whatever command we want then we command like so
Npm run [name like build]

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

What is React?

A

A JavaScript library for building user interfaces
It’s a package from npm

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

What is a React element?

A

It’s an object
React.createElement( ‘dom element in string’, [props, if nothing null], [children] )

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

How do you mount a React element to the DOM?

A

Create the html element where you want the object placed
Then const root = create a ReactDom.createRoot()
Then render method on that root var

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

What is Babel?

A

It’s a toolchain mainly used to convert ECMAScript 2015+ code into backwards compatible version of JavaScript in current and older browsers or environments, or back-covert JSX

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

What is a Plug-in?

A

This is a software component that adds a specific feature to an existing computer program. When a program supports plug-ins, it enables customization

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

What is a Webpack loader?

A

They are transformations that are applied to the source code of a module. They allow you to pre-process files as you “import” or “load” them.
Thus, they’re kind of like “tasks” and provide a powerful way to handle front-end build steps
Can transform files from different language like TypeScript to JavaScript or load inline images as data URLs.

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

How can you make Babel and Webpack work together?

A

You have to install babel-loader as a devDependency
Npm install babel-loader

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

What is JSX?

A

It’s a syntax extension to JavaScript
JavaScript plus more stuff (additional syntax), no HTML
Recommended to use with React to describe what the UI should look like, looking like template language but with the power of JavaScript

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

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

A

It actually creates an element, we just don’t see it with the JSX syntax
With JSX syntax it looks like
const element = <h1>Hello, JSX!</h1>;
But if you break it down, it looks like
Const element = React.createElement(‘ht’, null, “hello, JSX!’);
JSX creates a react element, which needs to be able to call on the React object, which has to be imported

17
Q

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

A

@babel/plugin-transform-react-jsx in devDependencies helps convert JSX into JavaScript
Webpack default behavior compiles everything and makes a module
Babel default behavior does nothing.
We have to tell babel exactly what to do with the transform-react-jsx plug-in

18
Q

What is a React component?

A

They’re reusable JavaScript functions or class that return React elements that represents a piece of the user interface They accept arbitrary inputs (properties) and then return React elements that describe what should appear on the screen

function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}

19
Q

How do you define a function component in React?

A

It can be arrow function or just function keyword, the first letter must be an uppercase letter and it needs to return a react element

function CustomButton() {
return <button>Click Me!</button>;
}

20
Q

How do you mount a component to the DOM?

A

You first document query select to where you want the element to show up
Then createRoot on ReactDOM with create the DOM element on that element you selected
Create a variable(element) and assign it the react element with the actual name of the function used to create the DOM element
Render the above element on the root variable first selected via querySelector ** the React element must be the argument for the render method on root

const container = document.querySelector(‘#root’);
const root = ReactDOM.createRoot(container);
const element = <CustomButton></CustomButton>;
root.render(element);

21
Q

What are props in React?

A

Props is an object
It can be used as properties/attributes
It is also used as the argument passed in a function to create customized attributes of a React element

22
Q

How do you pass props to a component?

A

You write props as the parameter in the function definition and then use props.[contentVariableName] the dot notation on props so that you can reference what to pass through this function. This is defined in a different variable
propName = value OR propName = { }
function CustomButton(props) {
return (
<button>{props.text}</button>
);
}

23
Q

How do you write JavaScript expressions in JSX?

A

Anything within curly braces can be a JavaScript expression in JSX
Variables
Actual math equations

24
Q

How do you create “class” component in React?

A

You must extend from React.Component and then call the render() method and return the React element
class CustomButton extends React.Component {
render() {
return <button>{this.props.text}</button>;
}
}

25
Q

How do you access props in a class component?

A

You define it in the React element with the this object
return <button>{this.props.text}</button>;

26
Q

What is the purpose of state in React?

A

It creates the base foundation of the default state of the React element that is used to compare for future changes
Data model of values that change over time
State is always what has happened, not what the user sees
I.e. The user clicks a button, that’s the action that happened. The text change is dependent on the user clicking the button
Always use facts to determine state

27
Q

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

A

Must bind() the method and value of this first
this.handleClick = this.handleClick.bind(this);
Then when you return the React element, you must pass the onClick property with the handleClick method to the element, like button)
<button onClick={this.handleClick}>Button Text Name</button>
You pass an event handler as a prop in the react element

28
Q

What are controlled components?

A

An input form element whose value is controlled by React, where React renders a form and controls what happens in that form on subsequent user input
Input value is controlled by the component and the component controls what happens to the value(?)

29
Q

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

A

Value property and onChange property
<input value={this.state.value} onChange={this.handleChange} />

30
Q

What Array method is commonly used to create a list of React elements?

A

Map method

31
Q

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

A

String (special string attribute you need to include when creating list of elements), must be unique in its scope