5 Essential React - Js Interview Questions and Answers PDF
5 Essential React - Js Interview Questions and Answers PDF
In the 2017 developer survey, Stack Overflow noted that React is still among
the the most popular JavaScript libraries to date. React has exploded in
popularity because its simple and declarative API produces highly-performant
applications — and that momentum only continues to grow.
If you’re looking to build a robust web application, chances are that React may
be a good fit for you. Once you're ready to hire a React developer, here are
essential interview questions to ask and some advanced concepts to know.
Before we dive right into the questions, it needs to be said that technical
interviews are notorious for gotcha-style questions and irrelevant
whiteboarding exercises. This article avoids that interview style entirely —
rather, I outlined five general (yet vital) concepts which I believe any seasoned
React developer should know.
Over the years, I have been in countless interviews as both the applicant and
the conductor. My experience has taught me that the best candidates for an
engineering role are those who can articulate intelligent opinions and defend
them using examples from their own experience. Pair-programming relevant
examples as a follow-upBytousing Codementor,
discussion would you
beagree to our Cookie
my preferred Policy. format,
interview ACCEPT
What is React?
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.
The answer to this question will likely vary depending on the candidate's
personal experiences. The important thing is to listen for real-life examples
provided and opinions on whether or not the candidate prefers React and why.
By comparing and contrasting React with another library, not only can the
candidate demonstrate a deep understanding of React, but also position
themself as a potentially strong candidate.
At the highest level, React components have lifecycle events that fall into three
general categories:
1. Initialization
2. State/Property Updates
3. Destruction
For example, a component may need to add event listeners to the DOM when it
first mounts. However, it should probably remove those event listeners when
the component unmounts from the DOM so that irrelevant processing does
not occur.
onResizeHandler() {
console.log('The window has been resized!');
}
}
Within these three general buckets exist a number of specific lifecycle hooks —
essentially abstract methods — that can be utilized by any React component to
more accurately manage updates. Understanding how and when these hooks
fire is key to building stable components and will enable you to control the
rendering process (improving performance).
By using Codementor, you agree to our Cookie Policy. ACCEPT
Take a look at the diagram above. The events under “Initialization” only happen
when a component is first initialized or added to the DOM. Similarly, the events
under “Destruction” only happen once (when the component is removed from
the DOM). However, the events under “Update” happen every time the
properties or state of the component change.
RELATED ARTICLES
29 AngularJS Interview
Questions and Answers You
Should Know
class MyComponent extends React.Component {
// only re-render if the ID has changed!
shouldComponentUpdate(nextProps, nextState) {
How Much Do Freelance
return nextProps.id === this.props.id;
Developers Cost Around the
} World? (2017)
}
return (
<div className="my-component">
<a href={props.url}>{props.name}</a>
</div>
);
}
}
Asking questions about JSX tests whether or not the candidate can state an
informed opinion towards JSX and defend it based on personal experience.
Let’s cover some of the basic talking points.
This is certainly true. Having said that, many React developers prefer to use JSX
as its syntax is far more declarative and reduces overall code complexity.
Facebook certainly encourages it in all of their documentation!
return (
<div className="my-component">
<AnotherClass {...props} />
</div>
);
}
}
But while ES2015 is becoming more and more widespread, it still is far from
widely supported by the major browsers — so tools like Babel or webpack are
needed to convert everything into legacy ES5 code.
Candidates that have built a React application using JSX and ES2015 can speak
about some specific pros or cons encountered, such as:
Although it took me some time to get used to the JSX and ES2015
syntax, I discovered how much I really enjoyed using it. Specifically,
I’m a big fan of…
The Flux pattern is generic; it’s not specific to React applications, nor is it
required to build a React app. However, Flux is commonly used by React
developers because React components are declarative — the rendered UI
(View) is simply a function of state (Store data).
By using Codementor, you agree to our Cookie Policy. ACCEPT
Flux is relatively simple in concept, but in a technical interview, it's important
that the developer demonstrates a deep understanding of its implementation.
Let’s cover of the important few discussion points.
Description of Flux
In the Flux pattern, the Store is the central authority for all data; any mutations
to the data must occur within the store. Changes to the Store data are
subsequently broadcast to subscribing Views via events. Views then update
themselves based on the new state of received data.
To request changes to any Store data, Actions may be fired. These Actions are
controlled by a central Dispatcher; Actions may not occur simultaneously,
ensuring that a Store only mutates data once per Action.
The strict unidirectional flow of this Flux pattern enforces data stability,
reducing data-related runtime errors throughout an application.
Flux vs MVC
Traditional MVC patterns have worked well for separating the concerns of data
(Model), UI (View) and logic (Controller)
By using — but
Codementor, youmany web
agree to ourdevelopers have
Cookie Policy. ACCEPT
discovered limitations with that approach as applications grow in size.
Specifically, MVC architectures frequently encounter two main problems:
Poorly defined data flow: The cascading updates which occur across views
often lead to a tangled web of events which is difficult to debug.
Lack of data integrity: Model data can be mutated from anywhere, yielding
unpredictable results across the UI.
With the Flux pattern complex UIs no longer suffer from cascading updates;
any given React component will be able to reconstruct its state based on the
data provided by the store. The flux pattern also enforces data integrity by
restricting direct access to the shared data.
During a technical interview, one should discuss the differences between the
Flux and MVC design patterns within the context of a specific example:
By using(1.x)
Difference with AngularJS Codementor, you agree to our Cookie Policy. ACCEPT
UI components in AngularJS typically rely on some internal $scope to store
their data. This data can be directly mutated from within the UI component or
anything given access to $scope — a risky situation for any part of the
component or greater application which relies on that data.
By contrast, the Flux pattern encourages the use of immutable data. Because
the store is the central authority on all data, any mutations to that data must
occur within the store. The risk of data pollution is greatly reduced.
Testing
One of the most valuable aspects of applications built on Flux is that their
components become incredibly easy to test. Developers can recreate and test
the state of any React component by simply updating the store — direct
interactions with the UI (with tools like Selenium) are no longer necessary in
many cases.
While Flux is a general pattern for enforcing data flow through an application,
there exist many implementations from which to choose from. There are
nuances between each implementation, as well as specific pros and cons to
consider. The candidate should provide examples of real-world experience with
using Flux.
// ---
ReactDOM.render(
<StatelessCmp name="Art" birthday="10/01/1980" />,
document.getElementById('main')
);
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.
componentDidMount() {
this.refs.myComponentDiv.addEventListener('click', this.clickHandler);
}
componentWillUnmount() {
this.refs.myComponentDiv.removeEventListener('click', this.clickHandler);
}
clickHandler() {
this.setState({
clicks: this.clicks + 1
});
}
render() {
let children = this.props.children;
return (
<div className="my-component" ref="myComponentDiv">
<h2>My Component ({this.state.clicks} clicks})</h2>
<h3>{this.props.headerText}</h3>
{children}
</div>
);
}
}
Given the code defined above, can you identify two problems?
1. The constructor does not pass its props to the super class. It should
include the following line:
constructor(props) {
super(props);
// ...
}
constructor(props) {
super(props);
this.clickHandler = this.clickHandler.bind(this);
// ...
}
Can you explain what the output of this class actually does? How would you
use it in an application?
This class creates a <div /> element and attaches a click listener to it. The
content of this component includes a <h2 /> element that updates every time
the user clicks on the parent <div /> , as well as an <h3 /> element
containing a provided title and whatever child elements were passed to it.
To use this class, the candidate should import it into another class and use it
like this:
Conclusion
Interviewing a React developer involves much more than just testing for React
knowledge — you should also ask questions about JavaScript and about other
nuances more closely related to the project or job in question.
This article attempted to cover some basic talking points that would
demonstrate whether or not a developer has adequate understanding of React
and its core concepts. I hope you find it useful — good luck!
Author Bio
Arthur Kay has been working with the Web since the late 1990s, when
GeoCities and scrolling marquees were all the rage. Arthur graduated from
Loyola University Chicago and now works as a Senior Software Engineer with a
core focus on JavaScript.
Codementor Team
On-Demand Marketplace for Software Developers
Our team is obsessed with learning about new technologies. We post
about development learning, step-by-step guides, technical tutorials, as
well as Codementor community announcements to help keep you up-to-
date.
Related Articles
Read more
Read more
Read more
CodementorX has top developers available for hire and freelance jobs. Check out our React
developers!
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
Correct me if I am wrong
In example of question 2
shouldComponentUpdate(nextProps, nextState) {
return nextProps.id === this.props.id;
}
https://facebook.github.io/...
1△ ▽ • Reply • Share ›
The Mystery of the Unbound This is not a React-specific issue. (In fact, neither is the
issue with super.) Although, if you wanted to test for this particular gotcha, I'd
probably recommend using the React event system: <div onclick="
{this.unboundHandler}">, where the unbound handler references an instance
property like this.state, which will throw an error.
4△ ▽ • Reply • Share ›