1. 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);
    }
    
  2. When component props defaults to true?

    If you pass no value for a prop, it defaults to true. This behavior is available so that it matches the behavior of HTML.

    For example, below expressions are equivalent,

    <MyInput autocomplete />
    
    <MyInput autocomplete={true} />
    

    Note: It is not recommended to use this approach because it can be confused with the ES6 object shorthand (example, {name} which is short for {name: name})

  3. What is NextJS and major features of it?

    Next.js is a popular and lightweight framework for static and server‑rendered applications built with React. It also provides styling and routing solutions. Below are the major features provided by NextJS,

    1. Server-rendered by default

    2. Automatic code splitting for faster page loads

    3. Simple client-side routing (page based)

    4. Webpack-based dev environment which supports (HMR)

    5. Able to implement with Express or any other Node.js HTTP server

    6. Customizable with your own Babel and Webpack configurations

  4. How do you pass an event handler to a component?

    You can pass event handlers and other functions as props to child components. It can be used in child component as below,

    <button onClick={this.handleClick}>
    
  5. 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

  6. How to prevent a function from being called multiple times?

    If you use an event handler such as onClick or onScroll and want to prevent the callback from being fired too quickly, then you can limit the rate at which callback is executed. This can be achieved in the below possible ways,

    1. Throttling: Changes based on a time based frequency. For example, it can be used using _.throttle lodash function

    2. Debouncing: Publish changes after a period of inactivity. For example, it can be used using _.debounce lodash function

    3. RequestAnimationFrame throttling: Changes based on requestAnimationFrame. For example, it can be used using raf-schd lodash function

  7. How JSX prevents Injection Attacks?

    React DOM escapes any values embedded in JSX before rendering them. Thus it ensures that you can never inject anything that’s not explicitly written in your application. Everything is converted to a string before being rendered.

    For example, you can embed user input as below,

    const name = response.potentiallyMaliciousInput;
    const element = <h1>{name}</h1>;
    

    This way you can prevent XSS(Cross-site-scripting) attacks in the application.

  8. How do you update rendered elements?

    You can update UI(represented by rendered element) by passing the newly created element to ReactDOM's render method.

    For example, lets take a ticking clock example, where it updates the time by calling render method multiple times,

    function tick() {
      const element = (
        <div>
          <h1>Hello, world!</h1>
          <h2>It is {new Date().toLocaleTimeString()}.</h2>
        </div>
      );
      ReactDOM.render(element, document.getElementById('root'));
    }
    
    setInterval(tick, 1000);
    
  9. How do you say that props are read only?

    When you declare a component as a function or a class, it must never modify its own props.

    Let us take a below capital function,

    function capital(amount, interest) {
       return amount + interest;
    }
    

    The above function is called “pure” because it does not attempt to change their inputs, and always return the same result for the same inputs. Hence, React has a single rule saying "All React components must act like pure functions with respect to their props."

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