React.js Basics Flashcards

React.js Basics

1
Q

When we render a list of components/elements, what property should we add to it?

A

key

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

The “key” property don’t get passed down to your components. What should we do if we need that value in our components?

A

We should explicitly pass the value as another prop

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

Which JS operators can be used to conditionally render components?

A
  • The && operator (expression && component)

- Ternary operator

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

What is a “Controlled Component”?

A

An input form element whose value is controlled by React.
That is achieved by passing a value via the component’s props, and using that props as the value of the input:
Ex:

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

What are 3 DOM elements that have internal state?

A
  • Input
  • Textarea
  • Select
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

To which event should you add a handler on a controlled component to handle mutation?

A

onChange

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

To which event should add a handler on a form to handle submission?

A

onSubmit

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

How is the “computed property name” ES6 syntax?

A

{ [prop] : value }

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

What happens if you specify the ‘value’ property when rendering a controlled component?
(eg: )

A

This will prevent the user to change the value of the component.

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

Explain the sentence: “The data always flows down”.

A

That means the state of a component can only be passed down to its children. A component can never pass state back to its parent.

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

How is the syntax for creating a new class component (with constructor and rendering methods)?

A
class Calculator extends React.Component {
  constructor(props) {    
  super(props);  
  }    
  render() {   
    return ( <p>Hello</p> )  
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

On the snippet:
return (<h1>MyValue</h1>);
Why is it recommended to put parentheses?

A

To avoid the pitfalls of JS’s automatic semicolon insertion.

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

What does Babel convert JSX expressions to?

A

To React.createElement() calls.

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

What is React’s equivalente to the following onclick event handling: Activate Lasers

A

Activate Lasers- Camel case event name- Function is passed as expression, not string

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

What is the difference between props and state for a component?

A

Props are properties that the component receives at creation time, and should be immutable.State are encapsulated properties that the component initializes (can be based on props values), and can mutate. State change causes component re-rendering.

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

What is the correct way to change a component’s state?

A

By calling this.setState()

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

In React what does “Lift the state up” means?

A

That is a technique to share state between components. Sharing state is accomplished by moving (lifting) it up to the closest common ancestor of the components that need it.The ancestor will then provide to the children both the state value and a handler function to mutate its state via the children props.

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

“There should be a single ‘source of truth’ for any data that changes in a React application.”“What does that quote mean in coding perspective?

A

Means that state should be owned by a single component. If the same state must be shared between components, it must be lifted up to an ancestral component.

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

Which tool can be used to inspect the components state in a page?

A

React Developer Tools

20
Q

What is the syntax of a function-based component?

A

function MyComponent(props) { return ( Hello );}

21
Q

Given: Content

How does the component DialogBox receives the div that is inside it?

A

Through the props.children property

22
Q
Given that a component SplitPane wants to render a layout:      
<div>
// left content here        
</div>
<div>
// right content here  
</div>

What is React solution to receive multiple children content?

A

Pass it normally via any attribute in props.
Ex:

}
right={ }
/>

SplitPane would use: this.props.left this.props.right internally

23
Q

Is there any limitations in what can be passed as props in React?

A

No.

24
Q

When is it recommended to use inheritance in React?

A

Never.

25
Q

What should I do if many components want to share the same non-UI related code?

A

Extract it into a separate JavaScript module The components may import it and use that function, object, or a class, without extending it.

26
Q

Which programming principle should be used when deciding when to create a new component?

A

Single Responsibility Principle

27
Q

Which lifecycle hook will be called on a component after it is rendered to the DOM for the first time?

A

componentDidMount()

28
Q

Which lifecycle hook will be called on a component whenever the DOM produced by it is removed?

A

componentDidUnmount()

29
Q

What are the 5 steps on Thinking in React?

A
  1. Start with the Mockup
  2. Break The UI Into A Component Hierarchy
  3. Build A Static Version in React - don’t use state
  4. Identify The Minimal (but complete) Representation Of UI State - the minimal state to be kept
  5. Identify Where Your State Should Live
  6. Add Inverse Data Flow - passing callbacks
30
Q

React uses one-way data binding, while Angular uses two-way data binding.That means in React the data flows automatically from the model to the UI only (using state mutation).How should the other way data flow (UI changing the model) be implemented in React?

A

The component that owns the state should pass ‘callback functions’ down to the components that will receive UI interaction.

31
Q

What are the steps that react do with virtual DOM at every state update?

A
  • generates a new virtual DOM with the changes- diffs it with the old one- computes the minimal set of DOM mutations and put them in a queue- batch updates all changes
32
Q

What is the benefit of using Functional Components over Class Components

A

These components tend to be easier to write and React will optimize them more in the future.

33
Q

How to pass a CSS class to a JSX component?

A

using the “className” attribute

34
Q

What is the correct way to set an int variable called “age” in this.state in the constructor

A

this.state = {age: 123}

not to be confused with this.state.age = 123

35
Q

How could you use JS destructuring to avoid having to type this.props. everytime?

A

const {propA, propB } = this.props;

36
Q

How could you use JS spread operator to pass all objects’s properties as props to a component?

A
37
Q

Which JS library can you use to facilitate routing inside your app?`

A

react-router

38
Q

Which React API facilitates passing down data to components implicitly, without using props?

A

The Context API

39
Q

Write a router that renders a component when the URL is at “/”

A
40
Q

Given a react-router Route with URL parameters:

How does MyApp components access the “filter” URL parameters?

A

The MyApp component will receive a parameter called “params”.

const MyApp( {params} ) => …

41
Q

Which react-router function can I use to create components that will have the router params injects?

A

withParam( myComponent )

42
Q

What is “PropTypes”?

A

It is a mechanism to describe accepted prop attributes of components, in order to make the code more type safe and also to document.

43
Q

How would you define the propTypes of a component Header, which accepts a string as “title” prop. The title must be required.

A

Header.propTypes = {
title: PropTypes.string.isRequired
}

44
Q

What are pure components (a.k.a. functional components)?

A

They are simple functions that don’t extend the React.Component class.

Ex:
const Component = (props) => {
  return (<h1>props.title</h1>);
}
45
Q

What is Flux?

A

Flux is a pattern for managing how data flows through a React application.

46
Q

Explain Flux

A

The major idea behind Flux is that there is a single-source of truth (the stores) and they can only be updated by triggering actions. The actions are responsible for calling the dispatcher, which the stores can subscribe for changes and update their own data accordingly.

47
Q

What is the difference between export and export default?

A

there can be only one “export default” per file. When you import it you do:
import AnyName from “myfile.js”

With normal export, you have to import with the same name. There can be many normal exports:
import { Name } from “myfile.js”