learn kro favicon img

interview questions for reactjs developer

1. How to perform automatic redirect after login?

Rendering a <Redirect> component, which is provided by react-router package, will navigate to a new location. Like server-side redirects, the new location will override the current location in the history stack.

2. How to format date using React Intl?

The injectIntl() higher-order component will give you access to the formatDate() method via the props in your component. The method is used internally by instances of FormattedDate and it returns the string representation of the formatted date.

3. Are there any similarities between Redux and RxJS?

Though both are used for different purposes, there are few similarities between them:

i) Redux is a tool for managing state throughout the application. It is usually used as an architecture for UIs, which can be presumed 50% as an alternative to Angular.

RxJS is a reactive programming library. It is usually used as a tool to accomplish asynchronous tasks in JavaScript. It can be presumed as an alternative to JavaScript Promises.

ii) Redux uses the Reactive paradigm because the Store is reactive. The Store observes actions from a distance, and changes itself.

RxJS also uses the Reactive paradigm, but instead of being an architecture, it gives us basic building blocks, Observables, to accomplish this pattern.

4. What is state in React?

In the React sense, “state” is an object that represents the parts of the app that can change. Each component can maintain its own state, which lives in an object called this.state.

State is private and fully controlled by the component. No other component can access the state except the one that owns it.

Example of a state:

5. How to pass a parameter to an event handler or callback?

Method 1:
Using arrow function

<button onClick={() => this.handleClick(id)} />

which is equivalent to call the bind() function

<button onClick={this.handleClick.bind(this, id)} />

Method 2:

Passing arguments to an array function

<button onClick={this.handleClick(id)} />
handleClick = (id) => () => { console.log(“Hello, your ticket number is”, id) };

6. What is the purpose of using super constructor with props argument?

A child class constructor cannot make use of this reference until super() method has been called. The same applies for ES6 sub-classes as well. The main reason for passing props parameter to super() call is to access this.props in the child constructors.

this.props is different only within the constructor. It would be the same outside the constructor.

Passing props:

Not passing props:

7. What is the impact of indexes as keys?

React needs to keep track of the elements, for that reason Keys should be always stable, unique & predictable.

Example:
The following code limits the React’s optimizations capability. Here each element’s key will be based on ordering, rather than tied to the data that is being represented.

But, if we use element data for unique key, assuming todo.id is unique to this list and stable, React would be able to reorder elements without needing to reevaluate them as much.

8. Do I need to rewrite all my class components with hooks?

Not necessarily. But we can try Hooks in a few components (or new components) without rewriting any existing code. Because there are no plans to remove classes in ReactJS.

9. Is Hooks cover all use cases for classes?

Hooks doesn’t cover all use cases of classes but there is a plan to add them soon. Currently, there are no Hook equivalents to the uncommon getSnapshotBeforeUpdate and componentDidCatch lifecycles yet.

10. How to ensure hooks followed the rules in your project?

We can ensure this by using ESLint plugin called eslint-plugin-react-hooks (released by React Team)

Two rules are there:

i) Add the plugin in the project (by executing the command line)

npm install [email protected]

ii) Applying the below config in ESLint config file

That’s all for today…Thank for Reading. Give a clap if you liked this article. Happy Learning 🙂

Leave a Comment

Your email address will not be published. Required fields are marked *