1. What is the typical use case of portals?

    React portals are very useful when a parent component has overflow: hidden or has properties that affect the stacking context(z-index,position,opacity etc styles) and you need to visually “break out” of its container.

    For example, dialogs, global message notifications, hovercards, and tooltips.

  2. How do you set default value for uncontrolled component?

    In React, the value attribute on form elements will override the value in the DOM. With an uncontrolled component, you might want React to specify the initial value, but leave subsequent updates uncontrolled. To handle this case, you can specify a defaultValue attribute instead of value.

    render() {
      return (
        <form onSubmit={this.handleSubmit}>
          <label>
            User Name:
            <input
              defaultValue="John"
              type="text"
              ref={this.input} />
          </label>
          <input type="submit" value="Submit" />
        </form>
      );
    }
    

    The same applies for select and textArea inputs. But you need to use defaultChecked for checkbox and radio inputs.

  3. What is your favorite React stack?

    Even though the tech stack varies from developer to developer, the most popular stack is used in react boilerplate project code. It mainly uses Redux and redux-saga for state management and asynchronous side-effects, react-router for routing purpose, styled-components for styling react components, axios for invoking REST api, and other supported stack such as webpack, reselect, ESNext, Babel. You can clone the project https://github.com/react-boilerplate/react-boilerplate and start working on any new react project.

  4. What is the difference between Real DOM and Virtual DOM?

    Below are the main differences between Real DOM and Virtual DOM,

    | Real DOM | Virtual DOM | | ----- | ------- | | Updates are slow | Updates are fast | | DOM manipulation is very expensive. | DOM manipulation is very easy | | You can update HTML directly. | You Can’t directly update HTML | | It causes too much of memory wastage | There is no memory wastage| | Creates a new DOM if element updates | It updates the JSX if element update|

  5. How to add Bootstrap to a react application?

    Bootstrap can be added to your React app in a three possible ways,

    1. Using the Bootstrap CDN: This is the easiest way to add bootstrap. Add both bootstrap CSS and JS resources in a head tag.
    2. Bootstrap as Dependency: If you are using a build tool or a module bundler such as Webpack, then this is the preferred option for adding Bootstrap to your React application
      npm install bootstrap
      
    3. React Bootstrap Package: In this case, you can add Bootstrap to our React app is by using a package that has rebuilt Bootstrap components to work particularly as React components. Below packages are popular in this category,
      1. react-bootstrap

      2. reactstrap

  6. Can you list down top websites or applications using react as front end framework?

    Below are the top 10 websites using React as their front-end framework,

    1. Facebook

    2. Uber

    3. Instagram

    4. WhatsApp

    5. Khan Academy

    6. Airbnb

    7. Dropbox

    8. Flipboard

    9. Netflix

    10. PayPal

  7. Is it recommended to use CSS In JS technique in React?

    React does not have any opinion about how styles are defined but if you are a beginner then good starting point is to define your styles in a separate *.css file as usual and refer to them using className. This functionality is not part of React but came from third-party libraries. But If you want to try a different approach(CSS-In-JS) then styled-components library is a good option.

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

    No. But you 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. How to fetch data with React Hooks?

    The effect hook called useEffect is used to fetch the data with axios from the API and to set the data in the local state of the component with the state hook’s update function.

    Let's take an example in which it fetches list of react articles from the API

    import React, { useState, useEffect } from 'react';
    import axios from 'axios';
    
    function App() {
      const [data, setData] = useState({ hits: [] });
    
      useEffect(async () => {
        const result = await axios(
          'http://hn.algolia.com/api/v1/search?query=react',
        );
    
        setData(result.data);
      }, []);
    
      return (
        <ul>
          {data.hits.map(item => (
            <li key={item.objectID}>
              <a href={item.url}>{item.title}</a>
            </li>
          ))}
        </ul>
      );
    }
    
    export default App;
    

    Remember we provided an empty array as second argument to the effect hook to avoid activating it on component updates but only for the mounting of the component. i.e, It fetches only for component mount.

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