React Flashcards

1
Q

What is the difference between state and props?

A

Props are immutable, a component’s state shouldn’t be changed from outside that component.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
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.

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

What happens during the lifecycle of a React component at a high level?

A

Initialization, State/Property Updates, Destruction

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

What are the different component lifecycle methods?

A

Initialization (getInitialState, getDefaultProps, componentWillMount, render, componentDidMount), Update (componentWillRecieveProps, shouldComponentUpdate, componentWillUpdate, render, componentDidUpdate), Destruction (componentWillUnmount)

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

What is JSX?

A

JSX embeds raw HTML templates inside JavaScript(ES2015) code. JSX code by itself cannot be read by the browser; it must be transpiled into traditional JavaScript using tools like Babel and webpack. JSX is not required to use React.

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

What is Flux?

A

Flux is an architectural pattern that enforces unidirectional data flow — its core purpose is to control derived data so that multiple components can interact with that data without risking pollution. (Action > Dispatch > Store > View). Store is single source of truth.

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

What are stateless components?

A

Stateless components (a flavor of “reusable” components) are nothing more than pure functions that render DOM based solely on the properties provided to them. This component has no need for any internal state — let alone a constructor or lifecycle handlers. The output of the component is purely a function of the properties provided to it.

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

What happens when you call setState?

A

The first thing React will do when setState is called is merge the object you passed into setState into the current state of the component. This will kick off a process called reconciliation. The end goal of reconciliation is to, in the most efficient way possible, update the UI based on this new state. To do this, React will construct a new tree of React elements (which you can think of as an object representation of your UI). Once it has this tree, in order to figure out how the UI should change in response to the new state, React will diff this new tree against the previous element tree. By doing this, React will then know the exact changes which occurred, and by knowing exactly what changes occurred, will able to minimize its footprint on the UI by only making updates where absolutely necessary.

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

What’s the difference between an Element and a Component in React?

A

Simply put, a React element describes what you want to see on the screen. Not so simply put, a React element is an object representation of some UI.

A React component is a function or a class which optionally accepts input and returns a React element (typically via JSX which gets transpiled to a createElement invocation).

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

When would you use a Class Component over a Functional Component?

A

If your component has state or a lifecycle method(s), use a Class component. Otherwise, use a Functional component.

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

What are refs in React and why are they important?

A

Similarly to keys, refs are added as an attribute to a React.createElement() call, such as <li>. The ref serves a different purpose, it provides us quick and simple access to the DOM Element represented by a React Element.

Refs can be either a string or a function. Using a string will tell React to automatically store the DOM Element as this.refs[refValue]

In order to use them as a function you add a ref attribute to your component whose value is a callback function which will receive the underlying DOM element or the mounted instance of the component as its first argument.</li>

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

What are keys in React and why are they important?

A

Keys are what help React keep track of what items have changed, been added, or been removed from a list. It’s important that each key be unique among siblings. Keys make reconciliation more efficient when dealing with lists because React can use the key on a child element to quickly know if an element is new or if it was just moved when comparing trees.

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

What is the difference between a controlled component and an uncontrolled component?

A

In a controlled component, React is the single source of truth for that input fields value and handles its changes. An uncontrolled component lets the DOM handle the field (like vanilla HTML) and we can access that value later with a ref. It’s typically recommended that you favor controlled components over uncontrolled components.

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

In which lifecycle event do you make AJAX requests and why?

A

AJAX requests should go in the componentDidMount lifecycle event.

There are a few reasons for this,

Fiber, the new React reconciliation algorithm, has the ability to start and stop rendering as needed for performance benefits. One of the trade-offs of this is that componentWillMount, the other lifecycle event where it might make sense to make an AJAX request, will be “non-deterministic”. What this means is that React may start calling componentWillMount at various times whenever it feels like it needs to. This would obviously be a bad formula for AJAX requests.

You can’t guarantee the AJAX request won’t resolve before the component mounts. If it did, that would mean that you’d be trying to setState on an unmounted component, which not only won’t work, but React will yell at you for. Doing AJAX in componentDidMount will guarantee that there’s a component to update.

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

What does shouldComponentUpdate do and why is it important?

A

Above we talked about reconciliation and what React does when setState is called. What shouldComponentUpdate does is it’s a lifecycle method that allows us to opt out of this reconciliation process for certain components (and their child components). Why would we ever want to do this? As mentioned above, “The end goal of reconciliation is to, in the most efficient way possible, update the UI based on new state”. If we know that a certain section of our UI isn’t going to change, there’s no reason to have React go through the trouble of trying to figure out if it should. By returning false from shouldComponentUpdate, React will assume that the current component, and all its child components, will stay the same as they currently are.

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

How do you tell React to build in Production mode and what will that do?

A

Typically you’d use Webpack’s DefinePlugin method to set NODE_ENV to production. This will strip out things like propType validation and extra warnings. On top of that, it’s also a good idea to minify your code because React uses Uglify’s dead-code elimination to strip out development only code and comments, which will drastically reduce the size of your bundle.

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

Why would you use React.Children.map(props.children, () => ) instead of props.children.map(() => )

A

It’s not guaranteed that props.children will be an array. React only makes props.children an array if there are more than one child elements. You want to favor React.Children.map because its implemention takes into account that props.children may be an array or an object.

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

Describe how events are handled in React.

A

In order to solve cross browser compatibility issues, your event handlers in React will be passed instances of SyntheticEvent, which is React’s cross-browser wrapper around the browser’s native event. These synthetic events have the same interface as native events you’re used to, except they work identically across all browsers.

What’s mildly interesting is that React doesn’t actually attach events to the child nodes themselves. React will listen to all events at the top level using a single event listener. This is good for performance and it also means that React doesn’t need to worry about keeping track of event listeners when updating the DOM.

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

What is the difference between createElement and cloneElement?

A

createElement is what JSX gets transpiled to and is what React uses to create React Elements (object representations of some UI). cloneElement is used in order to clone an element and pass it new props. They nailed the naming on these two

20
Q

What is the second argument that can optionally be passed to setState and what is its purpose?

A

A callback function which will be invoked when setState has finished and the component is re-rendered.

Something that’s not spoken of a lot is that setState is asynchronous, which is why it takes in a second callback function. Typically it’s best to use another lifecycle method rather than relying on this callback function, but it’s good to know it exists.

21
Q

What happens if you pass a function into this.setState?

A

It’s rarely used and not well known, but you can also pass a function to setState that receives the previous state and props and returns a new state

22
Q

Compare and contrast creating React Components in ES5 and ES2015 (also known as ES6)

A

ES5:
Creating React Components in ES5 uses the React.createClass() method and components can be rendered either inside another React Component or directly in the call to ReactDOM.render().

to set the state to an initial value, create the getInitialState() function on the Component

you can set the default props for the component to have a certain value with the getDefaultProps() method

ES5 version has autobinding

ES6 (ES2015):
Creating React Components in ES6 uses the class ... extends ... syntax

we have a constructor() that we use to set the initial state

add default props and a display name as properties of the new class created Component.defaultProps = {...}

no autobinding

23
Q

Explain the standard JavaScript toolchain, transpilation (via Babel or other compilers), JSX, and these items’ significance in recent development. What sort of tools might you use in the build steps to optimize the compiled output React code?

A

There are a couple primary pillars in the JavaScript toolchain: Dependency Management, Linting, Style-checking, Transpilation, and Compilation, Minification, Source-Mapping.

Style linting - typically a linter like JSCS is used to ensure the source code is following a certain structure and style

Dependency Management - for JavaScript projects, most people use other packages from npm; some plugins exist for build systems (e.g. Webpack) and compilers (e.g. Babel) that allow automatic installation of packages being imported or require()’d

Transpilation - a specific sub-genre of compilation, transpilation involves compiling code from one source version to another, only to a similar runtime level (e.g. ES6 to ES5)

Compilation - specifically separate from transpiling ES6 and JSX to ES5, is the act of including assets, processing CSS files as JSON, or other mechanisms that can load and inject external assets and code into a file. In addition, there are all sorts of build steps that can analyze your code and even optimize it for you.

Minification and Compression - typically part of - but not exclusively controlled by - compilation, is the act of minifying and compressing a JS file into fewer and/or smaller files

Source-Mapping - another optional part of compilation is building source maps, which help identify the line in the original source code that corresponds with the line in the output code (i.e. where an error occurred)

24
Q

componentWillMount()

A

Invoked once, both on the client and server, immediately before the initial rendering occurs. If you call setState within this method, render() will see the updated state and will be executed only once despite the state change.

25
Q

componentWillReceiveProps(object nextProps)

A

Invoked when a component is receiving new props. This method is not called for the initial render. Calling this.setState() within this function will not trigger an additional render. One common mistake is for code executed during this lifecycle method to assume that props have changed.

26
Q

componentWillUnmount()

A

Invoked immediately before a component is unmounted from the DOM. Perform any necessary cleanup in this method, such as invalidating timers or cleaning up any DOM elements that were created in componentDidMount.

27
Q

componentWillUpdate(object nextProps, object nextState)

A

Invoked immediately before rendering when new props or state are being received. This method is not called for the initial render.

28
Q

componentDidMount()

A

Invoked once, only on the client (not on the server), immediately after the initial rendering occurs. At this point in the lifecycle, you can access any refs to your children (e.g., to access the underlying DOM representation). The componentDidMount() method of child components is invoked before that of the parent component.

29
Q

componentDidUpdate(object prevProps, object prevState)

A

Invoked immediately after the component’s updates are flushed to the DOM. This method is not called for the initial render. Use this as an opportunity to operate on the DOM when the component has been updated.

30
Q

shouldComponentUpdate(object nextState, object nextProps)

A

Invoked before rendering when new props or state are being received. This method is not called for the initial render or when forceUpdate() is used. Use this as an opportunity to return false when you’re certain that the transition to the new props and state will not require a component update.

31
Q

Compare and contrast setState() and forceUpdate(). What is the significance of each, and when would you use one or the other?

A

setState() called within a Component will tell React to trigger the proper re-rendering. It will also invoke the lifecycle methods, and those methods’ control on the rendering process.

forceUpdate(), on the other hand, completely overrules the rendering process and queues up a new render for React to display to the screen; it will also not call shouldComponentUpdate().

In most cases, you should use setState() unless your code is setup to need a bypass for shouldComponentUpdate()

32
Q

What are propTypes?

A

Allows React to handle or restrict Props to certain types or requires certain Props to exist. Use these PropTypes to produce errors and track down bugs. When used effectively, PropTypes will prevent your team from losing too much time in the debugging and documentation process, ensuring stricter standards and understanding of your growing library of Components.

33
Q

Which feature can we use to cause a component to render only when its ID changes?

A

shouldComponentUpdate

34
Q

What are Higher Order Components?

A

Whereas a component transforms props into UI, a higher-order component transforms a component into another component. HOCs are common in third-party React libraries, such as Redux’s connect and Relay’s createFragmentContainer.

35
Q

In a general overview, how might React Router and its techniques differ from more traditional JavaScript routers like Backbone’s Router?

A

“Traditional” routers like the ever-popular Backbone.Router establish a predefined set of routes, in which each route defines a series of actions to take when a route is triggered. When combining Backbone.Router with React, you may have to mount and unmount React Components when the route changes

A Component has one or more Components as items in this.props.children, and s can have sub-s. React Router’s code recursively walks down the tree of children until there are no more to process, allowing the developer to recursively declare routes in a structure that encapsulates sub-routes, instead of having to implement a Backbone-esque flat list of routes (i.e. “/”, “/about”, “/users”, “/users/:id”, etc).

36
Q

How does React use the virtual DOM?

A

It quickly compares the current state of the UI with the desired state.
It then computes the minimal set of real DOM mutations to achieve the change.

37
Q

What is the package.json file?

A

A standard npm manifest file that holds various information about the project

38
Q

What are the 2 most important features of the package.json file?

A

Let’s you specify dependencies (that can get automatically downloaded and installed)
Let’s you define script tasks

39
Q

What does a module bundler do?

A

Modules help us organise JS code by splitting it into multiple files. The module bundler then automatically packs everything together in the correct load order

40
Q

What is webpack?

A

A module bundler that can also put source code through loaders that can transform and compile it

41
Q

What does our webpack.config.js file do?

A

require webpack and export a configuration object

42
Q

What are the bare minimum keys on the webpack configuration object?

A

entry and output

43
Q

What does the entry key point to on the webpack configuration object?

A

the main application module

44
Q

What does the output key do on the webpack configuration object?

A

tells webpack where to save the single JS file containing all the modules packed in the correct order

45
Q

What are props?

A

The mechanism used in React for passing data from parent to child components. The can’t be changed from inside the child component; props are passed and “owned” by the parent