FrontendDeveloper.in

React Interview Questions

  • Question 392

    What is the purpose of getDerivedStateFromError?

    This lifecycle method is invoked after an error has been thrown by a descendant component. It receives the error that was thrown as a parameter and should return a value to update state.

    The signature of the lifecycle method is as follows,

    static getDerivedStateFromError(error)
    

    Let us take error boundary use case with the above lifecycle method for demonstration purpose,

    class ErrorBoundary extends React.Component {
    constructor(props) {
    super(props);
    this.state = { hasError: false };
    }
    
    static getDerivedStateFromError(error) {
    // Update state so the next render will show the fallback UI.
    return { hasError: true };
    }
    
    render() {
    if (this.state.hasError) {
    // You can render any custom fallback UI
    return <h1>Something went wrong.</h1>;
    }
    
    return this.props.children;
    }
    }
    
  • Question 393

    What is the methods order when component re-rendered?

    An update can be caused by changes to props or state. The below methods are called in the following order when a component is being re-rendered.

    1. static getDerivedStateFromProps()
    2. shouldComponentUpdate()
    3. render()
    4. getSnapshotBeforeUpdate()
    5. componentDidUpdate()
  • Question 395

    What is the purpose of unmountComponentAtNode method?

    This method is available from react-dom package and it removes a mounted React component from the DOM and clean up its event handlers and state. If no component was mounted in the container, calling this function does nothing. Returns true if a component was unmounted and false if there was no component to unmount.

    The method signature would be as follows,

    ReactDOM.unmountComponentAtNode(container);
    
  • Question 396

    What are the limitations with HOCs?

    Higher-order components come with a few caveats apart from its benefits. Below are the few listed in an order,

    1. Don’t use HOCs inside the render method: It is not recommended to apply a HOC to a component within the render method of a component.
    render() {
    // A new version of EnhancedComponent is created on every render
    // EnhancedComponent1 !== EnhancedComponent2
    const EnhancedComponent = enhance(MyComponent);
    // That causes the entire subtree to unmount/remount each time!
    return <EnhancedComponent />;
    }
    

    The above code impacts on performance by remounting a component that causes the state of that component and all of its children to be lost. Instead, apply HOCs outside the component definition so that the resulting component is created only once.

    1. Static methods must be copied over: When you apply a HOC to a component the new component does not have any of the static methods of the original component
    // Define a static method
    WrappedComponent.staticMethod = function () {
    /*...*/
    };
    // Now apply a HOC
    const EnhancedComponent = enhance(WrappedComponent);
    
    // The enhanced component has no static method
    typeof EnhancedComponent.staticMethod === "undefined"; // true
    

    You can overcome this by copying the methods onto the container before returning it,

    function enhance(WrappedComponent) {
    class Enhance extends React.Component {
    /*...*/
    }
    // Must know exactly which method(s) to copy :(
    Enhance.staticMethod = WrappedComponent.staticMethod;
    return Enhance;
    }
    
    1. Refs aren’t passed through: For HOCs you need to pass through all props to the wrapped component but this does not work for refs. This is because ref is not really a prop similar to key. In this case you need to use the React.forwardRef API
  • Question 397

    How to debug forwardRefs in DevTools?

    React.forwardRef accepts a render function as parameter and DevTools uses this function to determine what to display for the ref forwarding component.

    For example, If you don't name the render function or not using displayName property then it will appear as ”ForwardRef” in the DevTools,

    const WrappedComponent = React.forwardRef((props, ref) => {
    return <LogProps {...props} forwardedRef={ref} />;
    });
    

    But If you name the render function then it will appear as ”ForwardRef(myFunction)”

    const WrappedComponent = React.forwardRef(function myFunction(props, ref) {
    return <LogProps {...props} forwardedRef={ref} />;
    });
    

    As an alternative, You can also set displayName property for forwardRef function,

    function logProps(Component) {
    class LogProps extends React.Component {
    // ...
    }
    
    function forwardRef(props, ref) {
    return <LogProps {...props} forwardedRef={ref} />;
    }
    
    // Give this component a more helpful display name in DevTools.
    // e.g. "ForwardRef(logProps(MyComponent))"
    const name = Component.displayName || Component.name;
    forwardRef.displayName = `logProps(${name})`;
    
    return React.forwardRef(forwardRef);
    }
    
  • Question 398

    Is it good to use arrow functions in render methods?

    Yes, You can use. It is often the easiest way to pass parameters to callback functions. But you need to optimize the performance while using it.

    class Foo extends Component {
    handleClick() {
    console.log("Click happened");
    }
    render() {
    return <button onClick={() => this.handleClick()}>Click Me</button>;
    }
    }
    

    Note: Using an arrow function in render method creates a new function each time the component renders, which may have performance implications

  • Question 399

    How do you say that state updates are merged?

    When you call setState() in the component, React merges the object you provide into the current state.

    For example, let us take a facebook user with posts and comments details as state variables,

    constructor(props) {
    super(props);
    this.state = {
    posts: [],
    comments: []
    };
    }
    

    Now you can update them independently with separate setState() calls as below,

    componentDidMount() {
    fetchPosts().then(response => {
    this.setState({
    posts: response.posts
    });
    });
    
    fetchComments().then(response => {
    this.setState({
    comments: response.comments
    });
    });
     }
    

    As mentioned in the above code snippets, this.setState({comments}) updates only comments variable without modifying or replacing posts variable.

  • Question 400

    How do you pass arguments to an event handler?

    During iterations or loops, it is common to pass an extra parameter to an event handler. This can be achieved through arrow functions or bind method.

    Let us take an example of user details updated in a grid,

    <button onClick={(e) => this.updateUser(userId, e)}>Update User details</button>
    <button onClick={this.updateUser.bind(this, userId)}>Update User details</button>
    

    In the both approaches, the synthetic argument e is passed as a second argument. You need to pass it explicitly for arrow functions and it will be passed automatically for bind method.

  • Question 401

    How to prevent component from rendering?

    You can prevent component from rendering by returning null based on specific condition. This way it can conditionally render component.

    function Greeting(props) {
    if (!props.loggedIn) {
    return null;
    }
    
    return <div className="greeting">welcome, {props.name}</div>;
    }
    
    class User extends React.Component {
    constructor(props) {
    super(props);
    this.state = {loggedIn: false, name: 'John'};
    }
    
    render() {
    return (
    //Prevent component render if it is not loggedIn
    <Greeting loggedIn={this.state.loggedIn} />
    <UserDetails name={this.state.name}>
    );
    }
    

    In the above example, the greeting component skips its rendering section by applying condition and returning null value.

  • Question 402

    Give an example on How to use context?

    Context is designed to share data that can be considered global for a tree of React components.

    For example, in the code below lets manually thread through a “theme” prop in order to style the Button component.

    //Lets create a context with a default theme value "luna"
    const ThemeContext = React.createContext("luna");
    // Create App component where it uses provider to pass theme value in the tree
    class App extends React.Component {
    render() {
    return (
    <ThemeContext.Provider value="nova">
    <Toolbar />
    </ThemeContext.Provider>
    );
    }
    }
    // A middle component where you don't need to pass theme prop anymore
    function Toolbar(props) {
    return (
    <ThemedButton />
    );
    }
    // Lets read theme value in the button component to use
    class ThemedButton extends React.Component {
    static contextType = ThemeContext;
    render() {
    return <Button theme={this.context} />;
    }
    }
    
  • Question 403

    How do you use contextType?

    ContextType is used to consume the context object. The contextType property can be used in two ways,

    1. contextType as property of class: The contextType property on a class can be assigned a Context object created by React.createContext(). After that, you can consume the nearest current value of that Context type using this.context in any of the lifecycle methods and render function.

    Lets assign contextType property on MyClass as below,

    class MyClass extends React.Component {
    componentDidMount() {
    let value = this.context;
    /* perform a side-effect at mount using the value of MyContext */
    }
    componentDidUpdate() {
    let value = this.context;
    /* ... */
    }
    componentWillUnmount() {
    let value = this.context;
    /* ... */
    }
    render() {
    let value = this.context;
    /* render something based on the value of MyContext */
    }
    }
    MyClass.contextType = MyContext;
    
    1. Static field You can use a static class field to initialize your contextType using public class field syntax.
    class MyClass extends React.Component {
    static contextType = MyContext;
    render() {
    let value = this.context;
    /* render something based on the value */
    }
    }
    
  • Question 404

    What is a consumer?

    A Consumer is a React component that subscribes to context changes. It requires a function as a child which receives current context value as argument and returns a react node. The value argument passed to the function will be equal to the value prop of the closest Provider for this context above in the tree.

    Lets take a simple example,

    <MyContext.Consumer>
    {value => /* render something based on the context value */}
    </MyContext.Consumer>
    
  • Question 405

    How do you solve performance corner cases while using context?

    The context uses reference identity to determine when to re-render, there are some gotchas that could trigger unintentional renders in consumers when a provider’s parent re-renders.

    For example, the code below will re-render all consumers every time the Provider re-renders because a new object is always created for value.

    class App extends React.Component {
    render() {
    return (
    <Provider value={{ something: "something" }}>
    <Toolbar />
    </Provider>
    );
    }
    }
    

    This can be solved by lifting up the value to parent state,

    class App extends React.Component {
    constructor(props) {
    super(props);
    this.state = {
    value: { something: "something" },
    };
    }
    
    render() {
    return (
    <Provider value={this.state.value}>
    <Toolbar />
    </Provider>
    );
    }
    }
    
Get LinkedIn Premium at Rs 399