avatarMohammad Faisal

Summary

The web content provides an in-depth exploration of advanced React concepts and questions that may be encountered in job interviews, emphasizing the importance of understanding deep framework concepts beyond simple syntax.

Abstract

The article titled "16 Conceptual React Questions to Stand Out in Your Next Interview" is a comprehensive guide aimed at React developers looking to enhance their knowledge and interview performance. It delves into complex topics such as Render Hijacking, synthetic events, portals, reconciliation, and React Fiber, offering insights into the inner workings of React. The piece underscores the significance of conceptual understanding over mere familiarity with syntax, suggesting that such knowledge distinguishes good developers from great ones. It also addresses practical aspects of React development, including the use of className over class, the advantages of fragments over container divs, and the management of state in uncontrolled components. The author provides examples and references to official React documentation, encouraging readers to explore further. The article concludes by discussing the limitations of React, the role of Hooks in relation to render props and higher-order components, and the immutability of props, reinforcing the idea that a deep grasp of React's philosophy and mechanisms can significantly benefit developers.

Opinions

  • The author believes that a developer's ability to answer conceptual questions reflects their dedication to understanding the framework deeply, which is crucial for standing out in job interviews.
  • Understanding documentation is presented as a key factor in differentiating between good and great developers.
  • The use of React's className over the HTML class attribute is justified due to class being a reserved keyword in JavaScript, highlighting the importance of adhering to React's conventions.
  • The author suggests that React Fragments are preferable to container divs for performance reasons and to maintain desired layout structures with CSS mechanisms like Flexbox and CSS Grid.
  • The article implies that while React is a powerful library, it has limitations, such as not being a full framework and having a learning curve for beginners, which developers should be aware of.
  • The author expresses that Hooks simplify the code and reduce nesting, making them a better choice than render props and higher-order components in many cases.
  • The immutability of props in React is emphasized as a core philosophy, guiding developers to treat props as read-only and to manage state changes appropriately.

16 Conceptual React Questions to Stand Out in Your Next Interview

Can you handle these ReactJS questions? Let’s find out!

Photo by Ryan Stone on Unsplash

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.

Well, 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, 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:

Can only update a mounted or mounting component.

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

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

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 is the way to go.

The syntax for this is

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.

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.

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 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.

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:

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.

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.

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!

Get in touch with me via LinkedIn or my Personal Website.

Resources:

Synthetic Events: https://reactjs.org/docs/events.html React Portal: https://reactjs.org/docs/portals.html React Fiber: https://github.com/acdlite/react-fiber-architecture

StackOverflow Forum: https://stackoverflow.com/questions/48144659/what-is-render-hijacking-in-react

More content at plainenglish.io

React
Interview
JavaScript
Web Development
Front End Development
Recommended from ReadMedium