React Flashcards

1
Q

What is React?

A

React is a declarative, efficient, and flexible JavaScript library for building user interfaces

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 plain objects that describe what you want to see on the screen.

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

Using the ReactDOM.render( ) method:

ReactDOM.render(element, container[, callback])

ReactDOM.render( ) method takes a JSX expression, creates a corresponding tree of DOM nodes, and adds that tree to the DOM

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

What is JSX?

A

JSX is a syntax extension to JavaScript that produces React “elements”

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

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

A

Because the React library must be in scope from your JSX code. It is not used explicitly but the compiler (Babel) uses it when converting JSX to React.createElement( )

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

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

A

By installing webpack, webpack-cli, babel-loader, @babel/core, @babel/plugin-transform-react-jsx to your packge.json devDependencies

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

What is a React component?

A

Components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.

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

How do you define a function component in React?

A
The simplest way to define a component is to write a JavaScript function.  
You can also use an ES6 class to define a component.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you mount a component to the DOM?

A

Using ReactDOM.render( ) method - use the function name as the type

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

What is the difference between a library and a framework?

A

A library is code that you call (lodash is a library, you call it when you want it to return something.)
A framework is code that is called by the framework (You do not call your own components, React calls your components.

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

What are props in React?

A

JSX attributes and children as a single object

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

How do you pass props to a component?

A

Add the prop to your JSX like you would add an attribute to an HTML tag

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

How do you write JavaScript expressions in JSX?

A

You can put any valid JavaScript expression (not statements) inside curly braces in JSX

e.g: {2 + 2} , {user.firstName} , {formatName(user)} etc.

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

How do you create “class” component in React?

A

To define a React component class, you need to extend React.Component and you must define the render( ) method.

Begin with the class keyword followed by the className followed by extends React.Component

e.g: class CustomButton extends React.Component {
  render( ) {
    return {this.props.text};
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you access props in a class component?

A

Use this to access props in a class component.

e.g: this.props.propName

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

What is the purpose of a state in React?

A

When the state object changes, the component re-renders depending on the state

So the component can render different things depending on the state

17
Q

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

A

By assigning it to a prop on the react element - the prop starts with on & is in camelCase - the event handler is in camelCase

18
Q

What is the virtual DOM?

A

The virtual DOM is a representation of a DOM object. React can save the virtual DOM so when the state updates and there is a re-render - react compares the old version and the new version and only replaces any differences (rather than deleting and re-creating the whole thing)

19
Q

What are controlled components?

A

An input form element whose value is controlled by React in that the value prop is saved in and based on the components state.

20
Q

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

A

value / onChange

21
Q

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

A

map ( ) method

22
Q

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

A

The best value for a “key” prop is a string that uniquely identifies a list item among its siblings (like an ID). Indexes are not recommended.

23
Q

When does React call a component’s componentDidMount method?

A

componentDidMount( ) is invoked immediately after a component is mounted (inserted into the tree).

If you need to load data from a remote endpoint, this is a good place to instantiate the network request.

24
Q

Name three React.Component lifecycle methods.

A

render( ), constructor( ), componentDidMount( ), componentDidUpdate( ), componentWillUnmount( ),

25
Q

How do you pass data to a child component?

A

Via props

26
Q

Why React?

A

React is fast and can handle complex updates and still feel quick and responsive.

React is modular - Instead of writing large, dense files of code, you can write many smaller, reusable files. React’s modularity can be a beautiful solution to JavaScript’s maintainability problems.

React is scalable. Large programs that display a lot of changing data are where React performs best

27
Q

Is a JSX element the same thing as an HTML element?

A

No A JSX element is not the same thing as an HTML element. A JSX element is a description of what we want to see on the page.

JSX elements are treated as JavaScript expressions and can go anywhere a JavaScript expression goes - saved to a variable, passed in a function, stored in an object or array…