FrontendDeveloper.in

React Interview Questions

  • Question 331

    What is the use of refs?

    The ref is used to return a reference to the element. They should be avoided in most cases, however, they can be useful when you need a direct access to the DOM element or an instance of a component.

  • Question 332

    How to create refs?

    There are two approaches

    1. This is a recently added approach. Refs are created using React.createRef() method and attached to React elements via the ref attribute. In order to use refs throughout the component, just assign the ref to the instance property within constructor.
    class MyComponent extends React.Component {
    constructor(props) {
    super(props);
    this.myRef = React.createRef();
    }
    render() {
    return <div ref={this.myRef} />;
    }
    }
    
    1. You can also use ref callbacks approach regardless of React version. For example, the search bar component's input element is accessed as follows,
    class SearchBar extends Component {
    constructor(props) {
    super(props);
    this.txtSearch = null;
    this.state = { term: "" };
    this.setInputSearchRef = (e) => {
    this.txtSearch = e;
    };
    }
    onInputChange(event) {
    this.setState({ term: this.txtSearch.value });
    }
    render() {
    return (
    <input
    value={this.state.term}
    onChange={this.onInputChange.bind(this)}
    ref={this.setInputSearchRef}
    />
    );
    }
    }
    

    You can also use refs in function components using closures. Note: You can also use inline ref callbacks even though it is not a recommended approach.

  • Question 333

    What are forward refs?

    Ref forwarding is a feature that lets some components take a ref they receive, and pass it further down to a child.

    const ButtonElement = React.forwardRef((props, ref) => (
    <button ref={ref} className="CustomButton">
    {props.children}
    </button>
    ));
    
    // Create ref to the DOM button:
    const ref = React.createRef();
    <ButtonElement ref={ref}>{"Forward Ref"}</ButtonElement>;
    
  • Question 334

    Which is preferred option with in callback refs and findDOMNode()?

    It is preferred to use callback refs over findDOMNode() API. Because findDOMNode() prevents certain improvements in React in the future.

    The legacy approach of using findDOMNode:

    class MyComponent extends Component {
    componentDidMount() {
    findDOMNode(this).scrollIntoView();
    }
    
    render() {
    return <div />;
    }
    }
    

    The recommended approach is:

    class MyComponent extends Component {
    constructor(props) {
    super(props);
    this.node = createRef();
    }
    componentDidMount() {
    this.node.current.scrollIntoView();
    }
    
    render() {
    return <div ref={this.node} />;
    }
    }
    
  • Question 335

    Why are String Refs legacy?

    If you worked with React before, you might be familiar with an older API where the ref attribute is a string, like ref={'textInput'}, and the DOM node is accessed as this.refs.textInput. We advise against it because string refs have below issues, and are considered legacy. String refs were removed in React v16.

    1. They force React to keep track of currently executing component. This is problematic because it makes react module stateful, and thus causes weird errors when react module is duplicated in the bundle.
    2. They are not composable — if a library puts a ref on the passed child, the user can't put another ref on it. Callback refs are perfectly composable.
    3. They don't work with static analysis like Flow. Flow can't guess the magic that framework does to make the string ref appear on this.refs, as well as its type (which could be different). Callback refs are friendlier to static analysis.
    4. It doesn't work as most people would expect with the "render callback" pattern (e.g. <DataGrid renderRow={this.renderRow} />)
    class MyComponent extends Component {
    renderRow = (index) => {
    // This won't work. Ref will get attached to DataTable rather than MyComponent:
    return <input ref={"input-" + index} />;
    
    // This would work though! Callback refs are awesome.
    return <input ref={(input) => (this["input-" + index] = input)} />;
    };
    
    render() {
    return (
    <DataTable data={this.props.data} renderRow={this.renderRow} />
    );
    }
    }
    
  • Question 336

    What are the different phases of component lifecycle?

    The component lifecycle has three distinct lifecycle phases:

    1. Mounting: The component is ready to mount in the browser DOM. This phase covers initialization from constructor(), getDerivedStateFromProps(), render(), and componentDidMount() lifecycle methods.

    2. Updating: In this phase, the component gets updated in two ways, sending the new props and updating the state either from setState() or forceUpdate(). This phase covers getDerivedStateFromProps(), shouldComponentUpdate(), render(), getSnapshotBeforeUpdate() and componentDidUpdate() lifecycle methods.

    3. Unmounting: In this last phase, the component is not needed and gets unmounted from the browser DOM. This phase includes componentWillUnmount() lifecycle method.

    It's worth mentioning that React internally has a concept of phases when applying changes to the DOM. They are separated as follows

    1. Render The component will render without any side effects. This applies to Pure components and in this phase, React can pause, abort, or restart the render.

    2. Pre-commit Before the component actually applies the changes to the DOM, there is a moment that allows React to read from the DOM through the getSnapshotBeforeUpdate().

    3. Commit React works with the DOM and executes the final lifecycles respectively componentDidMount() for mounting, componentDidUpdate() for updating, and componentWillUnmount() for unmounting.

    React 16.3+ Phases (or an interactive version)

    phases 16.4+

    Before React 16.3

    phases 16.2

  • Question 337

    What are the lifecycle methods of React?

    Before React 16.3

    • componentWillMount: Executed before rendering and is used for App level configuration in your root component.
    • componentDidMount: Executed after first rendering and here all AJAX requests, DOM or state updates, and set up event listeners should occur.
    • componentWillReceiveProps: Executed when particular prop updates to trigger state transitions.
    • shouldComponentUpdate: Determines if the component will be updated or not. By default it returns true. If you are sure that the component doesn't need to render after state or props are updated, you can return false value. It is a great place to improve performance as it allows you to prevent a re-render if component receives new prop.
    • componentWillUpdate: Executed before re-rendering the component when there are props & state changes confirmed by shouldComponentUpdate() which returns true.
    • componentDidUpdate: Mostly it is used to update the DOM in response to prop or state changes.
    • componentWillUnmount: It will be used to cancel any outgoing network requests, or remove all event listeners associated with the component.

    React 16.3+

    • getDerivedStateFromProps: Invoked right before calling render() and is invoked on every render. This exists for rare use cases where you need a derived state. Worth reading if you need derived state.
    • componentDidMount: Executed after first rendering and where all AJAX requests, DOM or state updates, and set up event listeners should occur.
    • shouldComponentUpdate: Determines if the component will be updated or not. By default, it returns true. If you are sure that the component doesn't need to render after the state or props are updated, you can return a false value. It is a great place to improve performance as it allows you to prevent a re-render if component receives a new prop.
    • getSnapshotBeforeUpdate: Executed right before rendered output is committed to the DOM. Any value returned by this will be passed into componentDidUpdate(). This is useful to capture information from the DOM i.e. scroll position.
    • componentDidUpdate: Mostly it is used to update the DOM in response to prop or state changes. This will not fire if shouldComponentUpdate() returns false.
    • componentWillUnmount It will be used to cancel any outgoing network requests, or remove all event listeners associated with the component.
  • Question 338

    How to create props proxy for HOC component?

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

    function HOC(WrappedComponent) {
    return class Test extends Component {
    render() {
    const newProps = {
    title: "New Header",
    footer: false,
    showFeatureX: false,
    showFeatureY: true,
    };
    
    return <WrappedComponent {...this.props} {...newProps} />;
    }
    };
    }
    
  • Question 339

    What is context?

    Context provides a way to pass data through the component tree without having to pass props down manually at every level.

    For example, authenticated users, locale preferences, UI themes need to be accessed in the application by many components.

    const { Provider, Consumer } = React.createContext(defaultValue);
    
  • Question 340

    What is the purpose of using super constructor with props argument?

    A child class constructor cannot make use of this reference until the super() method has been called. The same applies to ES6 sub-classes as well. The main reason for passing props parameter to super() call is to access this.props in your child constructors.

    Passing props:

    class MyComponent extends React.Component {
    constructor(props) {
    super(props);
    
    console.log(this.props); // prints { name: 'John', age: 42 }
    }
    }
    

    Not passing props:

    class MyComponent extends React.Component {
    constructor(props) {
    super();
    
    console.log(this.props); // prints undefined
    
    // but props parameter is still available
    console.log(props); // prints { name: 'John', age: 42 }
    }
    
    render() {
    // no difference outside constructor
    console.log(this.props); // prints { name: 'John', age: 42 }
    }
    }
    

    The above code snippets reveals that this.props is different only within the constructor. It would be the same outside the constructor.

  • Question 341

    How to set state with a dynamic key name?

    If you are using ES6 or the Babel transpiler to transform your JSX code then you can accomplish this with computed property names.

    handleInputChange(event) {
    this.setState({ [event.target.id]: event.target.value })
    }
    
  • Question 342

    What would be the common mistake of function being called every time the component renders?

    You need to make sure that function is not being called while passing the function as a parameter.

    render() {
    // Wrong: handleClick is called instead of passed as a reference!
    return <button onClick={this.handleClick()}>{'Click Me'}</button>
    }
    

    Instead, pass the function itself without parenthesis:

    render() {
    // Correct: handleClick is passed as a reference!
    return <button onClick={this.handleClick}>{'Click Me'}</button>
    }
    
  • Question 343

    What are error boundaries in React v16?

    Error boundaries are components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed.

    A class component becomes an error boundary if it defines a new lifecycle method called componentDidCatch(error, info) or static getDerivedStateFromError() :

    class ErrorBoundary extends React.Component {
    constructor(props) {
    super(props);
    this.state = { hasError: false };
    }
    
    componentDidCatch(error, info) {
    // You can also log the error to an error reporting service
    logErrorToMyService(error, info);
    }
    
    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;
    }
    }
    

    After that use it as a regular component:

    <ErrorBoundary>
    <MyWidget />
    </ErrorBoundary>
    
  • Question 345

    What is the purpose of render method of `react-dom`?

    This method is used to render a React element into the DOM in the supplied container and return a reference to the component. If the React element was previously rendered into container, it will perform an update on it and only mutate the DOM as necessary to reflect the latest changes.

    ReactDOM.render(element, container, [callback])
    

    If the optional callback is provided, it will be executed after the component is rendered or updated.

Get LinkedIn Premium at Rs 399