React Flashcards

1
Q

What is React

A
  • The V in MVC
  • Is ideal for large-scale SPA
  • Uses a high-speed virtual DOM (and updates the actual DOM only when needed)
  • Uses clean and easy-to-understand JSX syntax
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Props (vs State)

A
  • Props are passed to the child within the render method of the parent as the second argument to React.createElement()
  • Props (short for properties) are a Component’s configuration, its options if you may. They are received from above and immutable as far as the Component receiving them is concerned.
  • A Component cannot change its props, but it is responsible for putting together the props of its child Components.
  • Props are used to pass parameters which should be static (on the contrary of state). For example you can pass a size or name from an upperView to a lowerView (nested views);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

State (vs Props)

A
  • The state starts with a default value when a Component mounts and then suffers from mutations in time (mostly generated from user events). It’s a serializable (we didn’t say props are also serializable because it’s pretty common to pass down callback functions through props) representation of one point in time, a snapshot.
  • A Component manages its own state internally, but–besides setting an initial state–has no business fiddling with the state of its children. You could say the state is private.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Props vs State

A
  • Props and state are related. The state of one component will often become the props of a child component
  • Subcomponents cannot directly affect enclosing components, meaning data is flown downwards, updating data changes in HTML efficiently, and abstracts away the DOM allowing better performance using Virtual DOM.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

React vs ReactDOM

A

“As we look at packages like react-native, react-art, react-canvas, and react-three, it’s become clear that the beauty and essence of React has nothing to do with browsers or the DOM. To make this more clear and to make it easier to build more environments that React can render to, we’re splitting the main react package into two: react and react-dom.”

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

React.createElement() and ReactDOM.render()

A

To render a new tree into the DOM, you create ReactElements and pass them to ReactDOM.render along with a regular DOM element.

var root = React.createElement('div');
ReactDOM.render(root, document.getElementById('example'));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

React components

A

You can use React using only ReactElements but to really take advantage of React, you’ll want to use ReactComponents to create encapsulations with embedded state.

var MyComponent = React.createClass({
  render: function() {
    ...
  }
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

createClass vs ES6 class

A

You may also define your React classes as a plain JavaScript class. For example using ES6 class syntax

class HelloMessage extends React.Component {
  render() {
    return <div>Hello {this.props.name}</div>;
  }
}
ReactDOM.render(, document.getElementById('someId'));

ndlr: Au lieu du code suivant (exemple perso)

var HelloMessage = React.createClass({
  render: function() {
    return <div>Hello {this.props.name}</div>;
  }
});

ReactDOM.render(, document.getElementById(‘someId’));

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

createClass vs ES6 class

A

React.createClass is the workhorse of React. You can use it to model your components. At minimum it should implement a render method although it can implement various lifecycle hooks.

Alternatively you can use a ES6 type class. It’s more or less equal to createClass except that you cannot use mixins here. I prefer ES6 syntax myself as it hides some of the complexity and makes the API a bit tighter thanks to the usage of ES6 class constructor.

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

cloneElement()

A
  • When you clone the element, you can pass in props at that time
  • Note: cloneWithProps is deprecated. Use React.cloneElement instead.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Refs

A

The “don’t use refs” sentiment is correct when talking about using them for component instances. Meaning, you shouldn’t use refs as a way to grab component instances and call methods on them. This is the incorrect way to use refs and is when refs go south quickly.

The correct (and very useful) way to use refs is when you’re using them to get some value from the DOM. For example, if you have an input field attaching a ref to that input then grabbing the value later through the ref is just fine. Without this way, you need to go through a fairly orchestrated process for keeping your input field up to date with either your local state or your flux store - which seems unnecessary.

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

What is the difference between using constructor vs getInitialState

A

The two approaches are not interchangeable. You should initialize state in the constructor when using ES6 classes, and define the getInitialState method when using React.createClass.

```javascript
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = { /* initial state */ };
}
}
~~~

is equivalent to

```javascript
var MyComponent = React.createClass({
getInitialState() {
return { /* initial state */ };
},
});
~~~

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

Refs

A

Refs are used to access the real DOM and not the virtual DOM of react. It’s needed when you need to access the real DOM.

```javascript

this.refs.myInput
ReactDOM.findDOMNode(this.refs.myInput).focus()
ReactDOM.findDOMNode(this.refs.myInput).value
~~~

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

Reusable Components

A

When designing interfaces, break down the common design elements (buttons, form fields, layout components, etc.) into reusable components with well-defined interfaces. That way, the next time you need to build some UI, you can write much less code. This means faster development time, fewer bugs, and fewer bytes down the wire.

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

Props validation

A

As your app grows it’s helpful to ensure that your components are used correctly. We do this by allowing you to specify propTypes. React.PropTypes exports a range of validators that can be used to make sure the data you receive is valid. When an invalid value is provided for a prop, a warning will be shown in the JavaScript console.

```javascript
React.createClass({
propTypes: {
optionalArray: React.PropTypes.array,
optionalBool: React.PropTypes.bool,
optionalFunc: React.PropTypes.func,
optionalNumber: React.PropTypes.number,
optionalObject: React.PropTypes.object,
optionalString: React.PropTypes.string,
optionalSymbol: React.PropTypes.symbol,
}
})
~~~

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

Use state, not refs (for a form)

A

The short version: avoid refs.

They’re bad for maintainability, and lose a lot of the simplicity of the WYSIWYG model render provides.

People think refs are ‘easier’ than keeping it in state. This may be true for the first 20 minutes, it’s not true in my experience after that. Put your self in a position to say “Yeah, I’ll have it done in 5 minutes” rather than “Sure, I’ll just rewrite a few components”.