React Questions
Sources
- 5 Essential React Interview Questions
- Edureka: 50 React Interview Questions
- Pau1Fitz: React Interview
- React Resources
- sudheerj/reactjs-interview-questions
- pretty comprehensive - must revisit
- TopTal: 13 Essential React.js Interview Questions
- Tyler McGinnis: React Interview Questions
Topics
- Lifecycle Methods
Hard
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?
-
The bleeding edge JavaScript toolchain can seem quite complex, and it’s very important to feel confident in the toolchain and to have a mental picture of how the pieces fit together.
-
There are a couple primary pillars in the JavaScript toolchain: Dependency Management, Linting, Style-checking, Transpilation, and Compilation, Minification, Source-Mapping.
-
Typically, we use build tools like Gulp, Watchify/Browserify, Broccoli, or Webpack to watch the filesystem for file events (like when you add or edit a file). After this occurs, the build tool is configured to carry out a group of sequential or parallel tasks.
-
This part is the most complex piece, and is the center of the development process.
-
The rest of the tools belong in that group of sequential or parallel tasks:
- 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
import
ed orrequire()
‘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)
Compare and contrast the various React Component lifecycle methods. How might understanding these help build certain interfaces/features?
- There are several React lifecycle methods that help us manage the asynchronous and non-determinate nature of a Component during it’s lifetime in an app – we need provided methods to help us handle when a component is created, rendered, updates, or removed from the DOM.
- The “Will’s” - invoked right before the event represented occurs.
componentWillMount()
- 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.componentWillReceiveProps(object nextProps)
- 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.componentWillUnmount()
- 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.componentWillUpdate(object nextProps, object nextState)
- Invoked immediately before rendering when new props or state are being received. This method is not called for the initial render.
- The “Did’s”
componentDidMount()
- 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.componentDidUpdate(object prevProps, object prevState)
- 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.
- The “Should’s”
shouldComponentUpdate(object nextState, object nextProps)
- 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.
How do you use Hooks? Give some examples.
How do you use useEffect()
?
How do you use useMemo()
?
Intermediate
What are some of the most important Lifecycle Methods?
- componentWillMount() (deprecated) - Invoked once, on both client & server before rendering occurs.
- componentDidMount() - Invoked once, only on the client, after rendering occurs.
- componentWillReceiveProps() (deprecated) - Invoked as soon as the props are received from the parent class and before another render is called
- shouldComponentUpdate() - Returns true or false value based on certain conditions. If you want your component to update, return true else return false. By default, it returns false.
- componentWillUpdate() (deprecated) – Called just before rendering takes place in the DOM.
- componentDidUpdate() – Called immediately after rendering takes place.
- componentWillUnmount() – Called after the component is unmounted from the DOM. It is used to clear up the memory spaces.
- Here are all the lifecycle methods:
React Lifecycle Methods - https://github.com/coolinmc6/front-end-dev/blob/master/assets/react-lifecycle.png - fix asset path
Where in a React component should you make an AJAX request?
componentDidMount
is where an AJAX request should be made in a React component. This method will be executed when the component “mounts” (is added to the DOM) for the first time.- This method is only executed once during the component’s life. Importantly, you can’t guarantee the AJAX request will have resolved before the component mounts. If it doesn't, that would mean that you’d be trying to
setState
on an unmounted component, which would not work. - Making your AJAX request in
componentDidMount
will guarantee that there’s a component to update.
Explain Differences between State and Props.
Conditions | State | Props |
---|---|---|
Receive initial value from parent component | Yes | Yes |
Parent component can change value | No | Yes |
Set default values inside component | Yes | Yes |
Changes inside component | Yes | No |
Set initial value for child components | Yes | Yes |
Changes inside child components | No | Yes |
What are the differences between stateful and stateless components?
Stateful Component | Stateless Component |
---|---|
1. Stores info about component’s state change in memory | 1. Calculates the internal state of the components |
2. Have authority to change state | 2. Do not have the authority to change state |
3. Contains the knowledge of past, current and possible future changes in state | 3. Contains no knowledge of past, current and possible future state changes |
4. Stateless components notify them about the requirement of the state change, then they send down the props to them. | 4. They receive the props from the Stateful components and treat them as callback functions |
What are the differences between controlled and uncontrolled components?
Controlled Components | Uncontrolled Components |
---|---|
1. They do not maintain their own state | 1. They maintain their own state |
2. Data is controlled by the parent component | 2. Data is controlled by the DOM |
3. They take in the current values through props and then notify the changes via callbacks | 3. Refs are used to get their current values |
What are refs in React?
- Refs is the short hand for References in React.
- It is an attribute which helps to store a reference to a particular React element or component, which will be returned by the components render configuration function.
- It is used to return references to a particular element or component returned by
render()
. They come in handy when we need DOM measurements or to add methods to the components. - Here are some cases of when to use refs:
- When you need to manage focus, select text or media playback
- To trigger imperative animations
- Integrate with third-party DOM libraries
Links:
What is the significance of refs in React?
- Similarly to keys, refs are added as an attribute to a
React.createElement()
call, such as<li ref="someName"/>
. Theref
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]
. For example:
class List extends Component {
constructor(p){
super(p);
}
_printValue(){
console.log(this.refs.someThing.value);
}
render() {
return <div onClick={e => this._printValue()}>
<p>test</p>
<input type="text" ref="someThing" />
</div>
}
}
DOM.render(<List />, document.body);
this.refs.someThing
insidecomponentDidUpdate()
used to refer to a special identifier that we could use with React.findDOMNode(refObject) – which would provide us with the DOM node that exists on the DOM at this very specific instance in time. Now, React automatically attaches the DOM node to the ref, meaning thatthis.refs.someThing
will directly point to a DOM Element instance.- Additionally, a ref can be a function that takes a single input. This is a more dynamic means for you to assign and store the DOM nodes as variables in your code. For example:
class List extends Component {
constructor(p){
super(p)
}
_printValue(){
console.log(this.myTextInput.value)
}
render() {
return <div onClick={e => this._printValue()}>
<p>test</p>
<input type="text" ref={node => this.myTextInput = node} />
</div>
}
}
DOM.render(<List />, document.body);
Easy
What is React?
Version 1
- React is JavaScript library developed by Facebook.
- It is usually used as the view-layer of an application and one of its biggest benefits is that it provides a performance boost
- It does that by introducing a concept called the Virtual DOM that works by selectively rendering parts of your application depending on state changes. It does the least amount of DOM manipulation possible to keep your components up-to-date
Version 2
- React is a JavaScript library developed by Facebook
- Its main purpose is to build user interfaces; so just what you see on the front-end
- React makes that process easy by breaking every page down into pieces called components
Version 3
- React is a front end JavaScript library developed by Facebook in 2011.
- It follows the component based approach which helps in building reusable UI components. It is used for developing complex and interactive web and mobile UI.
- Even though, it was open-sourced only in 2015, it has one of the largest communities supporting it.
What do you know about React? Summarize your knowledge.
- React is the view-layer of an application and has an interesting way of handling the 'state management' aspect of your application.
- The 'state management' problem I am referring to is let's say you want to show the same data in several places. How do you ensure they are consistent across your application?
- It introduces the concept of the Virtual DOM which sits on top of the actual DOM.
- Usually this means that the entire page or application is rendered to a particular render target like a
<div>
- Usually this means that the entire page or application is rendered to a particular render target like a
- Handling DOM manipulation with a Virtual DOM leads to a performance boost BUT it is a big file to load
- React utilizes a lot of ES6's new features and syntax and is typically written in what's called JSX which is a sort of HTML-JavaScript hybrid
- A JSX component is usually a function that returns a parent element (i.e. a div) which inside it has mostly normal HTML (for example, instead of "class" they "className"
- Building a React page is all about components. There is usually a parent
<App />
component and then inside them you put more components that you build
How does React work?
- React creates a virtual DOM. When state changes in a component it firstly runs a "diffing" algorithm, which identifies what has changed in the virtual DOM. The second step is reconciliation, where it updates the DOM with the results of diff.
What are some of the major features of React?
- It uses the virtual DOM instead of the real DOM.
- It uses server-side rendering.
- It follows uni-directional data flow or data binding.
What are some advantages of React?
- It increases the application’s performance
- It can be conveniently used on the client as well as server side
- Because of JSX, code’s readability increases
- React is easy to integrate with other frameworks like Meteor, Angular, etc
- Using React, writing UI test cases become extremely easy
What are some limitations of React?
- React is just a library, not a full-blown framework
- Its library is very large and takes time to understand
- It can be little difficult for the novice programmers to understand
- Coding gets complex as it uses inline templating and JSX
What is JSX?
- JSX is the JavaScript-HTML hybrid that you write React in.
- JSX allows you to embed JavaScript expressions into your code by wrapping it in curly braces. This includes something like '2+2' or printing the
age
property from yourperson
object. - JSX can look like HTML but attributes in your elements use camelCase and often have different attribute names (i.e.
className
instead of justclass
) - JSX compiles down to
React.createElement()
calls using Babel. So as I said, JSX is really just a user-friendly way to create these JavaScript objects that React uses to build the virtual DOM. - Browsers can't read JSX because it is not JavaScript. For React to work, JSX must be transformed into JavaScript using a JSX transformer like Babel and then passed to the browser.
How does the Virtual DOM work?
Person Analogy
- Imagine you have an object that you want to model around a person. And this object has every relevant property that a person could possibly have (i.e. arms, legs, head, etc.)
- This is basically what React is doing with the DOM. It is building a virtual DOM with all of the relevant properties of the actual DOM.
- Now sticking with the Person object example, let's say you wanted to add a mustache, change their eyes to green, and give them some big biceps.
- So when we apply these changes, what React does is it FIRST runs a "diffing" algorithm which pretty much just identifies what has changed with your person object.
- Second, it runs a reconciliation process where it updates the actual DOM (or in this case, the person) with just the results of the diffing algorithm.
- This means that instead of rebuilding your real person from the ground up, React would only change the real person's face (mustache and green eyes) and their arms (bigger biceps).
- And what's cool about this is because React is doing all this on a virtual DOM is that these views can be rendered server-side.
Version #2
- A virtual DOM is a JavaScript object that is essentially a copy for the real DOM.
- The object itself is just a node tree with elements and their attributes and contents (as Objects) along with their properties
- It then updates this tree based on changes in the data model (or state) of the application.
- These changes can come from the user (i.e. user actions like a tweet or a like) or from the system (new tweet comes in, friend request, etc.)
- Whenever there is a data change, the entire UI is re-rendered in the Virtual DOM
- React then compares that virtual DOM to the real DOM in its diffing algorithm React then updates only the part of the real DOM that has been changed
Real DOM vs. Virtual DOM
Real DOM | Virtual DOM |
---|---|
It updates slow. | It updates faster. |
Can directly update HTML. | Can’t directly update HTML. |
Creates a new DOM if element updates. | Updates the JSX if element updates. |
DOM manipulation is very expensive. | DOM manipulation is very easy. |
Too much of memory wastage. | No memory wastage. |
How does React render the virtual DOM?
- Usually you have a root HTML element in your
index.html
file that has an id of 'root' but I've also seen other names. This div is called the "root" DOM node because everything in it is managed by React DOM. - To render an element, we call the
ReactDOM.render()
method which takes two parameters: the element you want to render and the root DOM node. - The element you want to render usually houses the entire application so it'll typically be called
App
or something like that - The root DOM node is actually selected using JavaScript:
document.getElementById('root')
- Usually, the
ReactDOM.render()
method is called once and then the state of the app changes which causes the components to re-render - React DOM compares the element and its children to the previous version of it and then updates what has changed
Explain the sentence: "In React, everything is a component."
- Components are the building blocks of a React application's user interface (UI).
- These components split the entire UI into small, independent and reusable pieces.
- React then renders these components independent of each other without affecting the UI.