16 React Interview Questions
Tags: javascript, react, programming, productivity
Published: 2023-10-17 18:05:17

To read more articles like this, [visit my blog](https://www.mdfaisal.com/blog)


Answering a conceptual question in an interview doesn’t mean you will be better than others on the job, but it surely means you have the time and interest to understand the deep concepts in a framework.

>  One’s understanding of documentation separates a good developer from a great developer.

Today, we will look into some of these conceptual questions that you may want to know as a React developer. If you are looking for simple syntax-related questions, then this article is not for you!

Let’s get started!

## 1. What is Render Hijacking in React?

[As Mr. Google says](https://stackoverflow.com/questions/48144659/what-is-render-hijacking-in-react), Render Hijacking is

>  “The concept of render hijacking is the ability to control what a component will output from another component”.

Practically you wrap your component with another Higher-Order-Component (HOC). And then, you inject props according to your need that can cause a change in the rendering logic.

Basically, what you are doing is enabling the component to behave differently.

## 2. What will happen if you use setState() in the constructor?

When you use `setState()` React re-renders the whole component once again. So if you call `setState()` inside a constructor, React tries to re-render the component that does not exist, creating a recursive problem.

You would get errors like this:

```js
Can only update a mounted or mounting component.
```

So we need to use `this.state` to initialize variables inside the constructor. Like the following

```js
constructor(props) {
    this.state = {
        // anything that you want inside state
    }
}
```

## 3. What are synthetic events in React?

Events are any browsers essentially parts. Like `onclick` , `onscroll` Etc. But as React works with a virtual dom, we need to have a wrapper that works consistently across different browsers.

>  React normalizes events so that they have consistent properties across different browsers. — [React Documentation](https://reactjs.org/docs/events.html)


So, in summary, synthetic events are a cross-browser wrapper for different events. Important to note: they are not mapped one-to-one with browsers native events.

## 4. What are portals in React?

If you want to render some child component into a DOM node outside the component tree hierarchy, then [React Portal](https://reactjs.org/docs/portals.html) is the way to go.

The syntax for this is

```JS
ReactDOM.createPortal(child, container)
```

The first argument is any render-able React child, such as an element, string, or fragment. The second argument is a DOM element.

## 5. What is reconciliation?

When a component’s props or state change, We need to re-render the component. Whether the actual DOM will be updated or not depends on the difference between these two nodes (previous and current).

But to compare 2 nodes, we will need an O(n³) complexity which is not practical in real-life scenarios. That’s why React team has decided to use a heuristic approach.

And the name of this process is called reconciliation. If you are interested, refer to the [documentation](https://reactjs.org/docs/portals.html).

## 6. Why does React uses className over the class?

`class` Is a keyword in JavaScript, and JSX is an extension of JavaScript. That’s the principal reason why React uses className instead of class.

```JS

render() {   
  return <span className={'menunavigation-menu'}>{'Menu'}</span>
}
```

Pass a string as the className prop.

## 7. What is React Fiber?

Fiber is the new reimplementation of the core algorithm in React v16. We already talked about reconciliation, and the new implementation is called React Fiber.

[Here](https://github.com/acdlite/react-fiber-architecture) is great documentation for you to follow if you are interested in understanding the core concepts.

## 8. What is the Goal of React Fiber?

The goal of React Fiber is to increase its suitability for areas like animation, layout, and gestures. Its headline feature is incremental rendering: the ability to split rendering work into chunks and spread it out over multiple frames.

## 9. What are uncontrolled components?

The normal way to store state in a react component is `useState` or `this.state` . But one problem is that they are tied to the rendering process. As a result, in some cases, you face some difficulty.

If you store the state of a component using, ref then that component is called an uncontrolled component. it’s more like traditional HTML

In the below `UserProfile` component, the name input is accessed using ref.

```js
class UserProfile extends React.Component {
    constructor(props) {
        super(props)
        this.handleSubmit = this.handleSubmit.bind(this)
        this.input = React.createRef()
    }
    handleSubmit(event) {
        alert('A name was submitted:' + this.input.current.value)
        event.preventDefault()
    }
    render() {
        return <div></div>
    }
}
```

## 10. Why are fragments better than container divs?

Below is the list of reasons,

1. Fragments are a bit faster and use less memory by not creating an extra DOM node. This only has a real benefit on huge and deep trees.

2. Some CSS mechanisms like Flexbox and CSS Grid have a special parent-child relationship, and adding divs in the middle makes it hard to keep the desired layout.

3. The DOM Inspector is less cluttered.

## 11. What are the limitations of React?

Apart from the advantages, there are few limitations of React too,

 1. React is just a view library, not a full framework.

 2. There is a learning curve for beginners who are new to web development.

 3. Integrating React into a traditional MVC framework requires some additional configuration.

 4. The code complexity increases with inline templating and JSX.

 5. Too many smaller components leading to over-engineering or boiler-plate.

## 12. How to create props proxy for HOC component?

You can add/edit props passed to the component using a props proxy pattern like this:

```js
function HOC(WrappedComponent) {
    return class Test extends React.Component {
        render() {
            const newProps = { title: 'New Header', footer: false, showFeatureX: false, showFeatureY: true }
            return <WrappedComponent {...this.props} {...newProps} />
        }
    }
}
```

## 13. What happens if you use props in the initial state?

The `constructor` function never updates the current state of the component. It’s called only once before the component is mounted.

So let’s say we are using props to initialize the state of a component.

```js
function SomeComponent(props){

    const [count , setCount]= useState(props.count);
}
```

Now if the `props.count` changes from the parent, then the child will never be updated because the `useState` was called only once. So it’s not a good idea to pass props to initialize state.

## 14. Do Hooks replace render props and higher-order components?

Both render props and higher-order components render only a single child, but in most cases, Hooks are a simpler way to serve this by reducing nesting in your tree.

## 15. Can you force a component to re-render without calling setState?

Whenever your component's state or props change, the component will re-render. But if, for some reason, we have to force a component to re-render, then what we can call the `foreUpdate()` function.

```js
component.forceUpdate(callback)
```

## 16. Why you can’t update props in React?

The React philosophy is that props should be immutable and top-down. This means that a parent can send any prop values to a child but can’t modify received props.

That’s it. Maybe you knew some of them or maybe you didn’t. I think (and hope) you have learned a thing or two from this article.

Have a great day!

### Resources:

* Synthetic Events: [**https://reactjs.org/docs/events.html](https://reactjs.org/docs/events.html)

* React Portal: **[https://reactjs.org/docs/portals.html](https://reactjs.org/docs/portals.html)

* React Fiber:** [https://github.com/acdlite/react-fiber-architecture](https://github.com/acdlite/react-fiber-architecture)

* StackOverflow Forum: [**https://stackoverflow.com/questions/48144659/what-is-render-hijacking-in-react](https://stackoverflow.com/questions/48144659/what-is-render-hijacking-in-react)


**Have something to say? Get in touch with me via [LinkedIn](https://www.linkedin.com/in/56faisal/) or [Personal Website](https://www.mdfaisal.com/)**