Basics Flashcards

1
Q

What are React elements?

A

Elements are what components are “made of”

Element —> <div></div>

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

What are components?

A
  • Reusable pieces that make up UI
  • Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.
  • Such functions are called “pure” because they do not attempt to change their inputs, and always return the same result for the same inputs.
  • Components must return a single root element.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Props

A
  • When React sees an element representing a user-defined component, it passes JSX attributes to this component as a single object. We call this object “props”.
  • React is pretty flexible but it has a single strict rule: All React components must act like pure functions with respect to their props
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

React is pretty flexible but it has a single strict rule, what is it?

A

All React components must act like pure functions with respect to their props.

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

What are pure functions?

A

They always return the same result for the same inputs.

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

How is state different from props?

A

State is similar to props, but it is private and fully controlled by the component.

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

What is an additional feature given to components defined as classes?

A

Local state is exactly that: a feature available only to classes.

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

What is a class constructor?

A

The constructor method is a special method for creating and initializing an object created with a class. There can only be one special method with the name “constructor” in a class. A SyntaxError will be thrown if the class contains more than one occurrence of a constructor method.

A constructor can use the super keyword to call the constructor of a parent class.

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  get area() {
    return this.calcArea();
  }

calcArea() {
return this.height * this.width;
}
}

const square = new Rectangle(10, 10);

console.log(square.area);

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

Where is state defined?

A

In the class constructor

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

What is mount and unmount?

A

mount - is rendered to the DOM for the first time. This is called “mounting” in React.

unmount - whenever the DOM produced by the component is removed. This is called “unmounting” in React.

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

What are three things you should know about setState()

A
  • Do Not Modify State Directly
    For example, this will not re-render a component:
// Wrong
this.state.comment = 'Hello';
Instead, use setState():
// Correct
this.setState({comment: 'Hello'});
The only place where you can assign this.state is the constructor.
  • State Updates May Be Asynchronous
    React may batch multiple setState() calls into a single update for performance.

Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.

For example, this code may fail to update the counter:

// Wrong
this.setState({
  counter: this.state.counter + this.props.increment,
});
To fix it, use a second form of setState() that accepts a function rather than an object. That function will receive the previous state as the first argument, and the props at the time the update is applied as the second argument:
// Correct
this.setState((prevState, props) => ({
  counter: prevState.counter + props.increment
}));

State Updates are Merged

When you call setState(), React merges the object you provide into the current state.

For example, your state may contain several independent variables:

  constructor(props) {
    super(props);
    this.state = {
      posts: [],
      comments: []
    };
  }
Then you can update them independently with separate setState() calls:
  componentDidMount() {
    fetchPosts().then(response => {
      this.setState({
        posts: response.posts
      });
    });
    fetchComments().then(response => {
      this.setState({
        comments: response.comments
      });
    });
  }
The merging is shallow, so this.setState({comments}) leaves this.state.posts intact, but completely replaces this.state.comments.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When does componentDidMount() run?

A

after the component output has been rendered to the DOM.

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

If you don’t use something in render(), it should it be in the state?

A

No

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

Why state is often called local or encapsulated

A

It is not accessible to any component other than the one that owns and sets it.

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

What does this mean? –> import React from ‘react’

A

Import everything in the react module into the variable React

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

What two parameters does the render method in react-dom take?

A

component to render,

What DOM element it should render to

17
Q

How to comment in JSX?

A

{/* */}

18
Q

How do you format a Stateless functional component

A
const Header = (props) => {
  return (
)
}

//you don’t need this.props becuase it is not a react component, it is just a function

19
Q

How do you setup react router?

A

import { BrowserRouter, Match, Miss}

const Root = ()=> {
return (

<div>

</div>

)
}

20
Q

Why do you have to bind methods to react components in es6 classes?

A

Render method is the only method that binds to the component. Any other methods that use this will not automatically bind to the component. The keyword this inside the render method is bound to the component. the keyword this in methods defined in the es6 class are not bound to anything.

To fix this you must bind the the method to the component in a constructor method, bind it inside the render method, or define it in a function like this–> onSubmit={(e) => this.goToStore(e)}

21
Q

What are Controlled Components?

A

In HTML, form elements such as , , and typically maintain their own state and update it based on user input. In React, mutable state is typically kept in the state property of components, and only updated with setState().

We can combine the two by making the React state be the “single source of truth”. Then the React component that renders a form also controls what happens in that form on subsequent user input.

22
Q

How do you update a property of an object in state?

A

this.setState({
result: Object.assign({}, this.state.result, { hits: updatedHits })
});

OR

const hits = this.state.result.hits.filter(isNotId);
    this.setState({
      result: { ...this.state.result, hits: hits }    
    });
23
Q

How would you conditionally render a component or element from within the render method?

A

{result
?
: null
}

OR

{ result &&

}

24
Q

Pretend you are in the Table component. How would you assign variable names to all the passed props into table using es6?

A

const { list, pattern, onDismiss, isSearched } = this.props;

25
Q

When is the render method called?

A

The render() method is called during the mount process too, but also when the component updates.

26
Q

What are the 4 steps of the mounting process?

A
  1. constructor()
  2. componentWillMount()
  3. render()
  4. componentDidMount()
27
Q

How do you update a property of an object in state?

A

this.setState({
result: Object.assign({}, this.state.result, { hits: updatedHits })
});

28
Q

How would you conditionally render a component or element from within the render method?

A

{result
?
: null
}

29
Q

How do you prevent default behavior of a form in react?

A

event.preventDefault();

30
Q

How do you import a variable as an alias?

A

import { firstname as foo } from ‘./file1.js’;

31
Q

How would you write proptypes for this stateless functional component?

const Button = ({ onClick, className = ‘’, children,}) =>

{children}
A

import PropTypes from ‘propTypes’

Button.propTypes = {
onClick: PropTypes.func,
className: PropTypes.string,
children: PropTypes.node,
};
32
Q

What is React?

A

React is an open-source JavaScript library created by Facebook for building complex, interactive UIs in web and mobile applications.

The key point in this answer is that React’s core purpose is to build UI components; it is often referred to as just the “V” (View) in an “MVC” architecture. Therefore it has no opinions on the other pieces of your technology stack and can be seamlessly integrated into any application.