FrontendDeveloper.in

React Interview Questions

  • Question 346

    What will happen if you use `setState()` in constructor?

    When you use setState(), then apart from assigning to the object state React also re-renders the component and all its children. You would get error like this: Can only update a mounted or mounting component. So we need to use this.state to initialize variables inside constructor.

  • Question 347

    Is it good to use `setState()` in `componentWillMount()` method?

    Yes, it is safe to use setState() inside componentWillMount() method. But at the same it is recommended to avoid async initialization in componentWillMount() lifecycle method. componentWillMount() is invoked immediately before mounting occurs. It is called before render(), therefore setting state in this method will not trigger a re-render. Avoid introducing any side-effects or subscriptions in this method. We need to make sure async calls for component initialization happened in componentDidMount() instead of componentWillMount().

    componentDidMount() {
    axios.get(`api/todos`)
    .then((result) => {
    this.setState({
    messages: [...result.data]
    })
    })
    }
    
  • Question 348

    What will happen if you use props in initial state?

    If the props on the component are changed without the component being refreshed, the new prop value will never be displayed because the constructor function will never update the current state of the component. The initialization of state from props only runs when the component is first created.

    The below component won't display the updated input value:

    class MyComponent extends React.Component {
    constructor(props) {
    super(props);
    
    this.state = {
    records: [],
    inputValue: this.props.inputValue,
    };
    }
    
    render() {
    return <div>{this.state.inputValue}</div>;
    }
    }
    

    Using props inside render method will update the value:

    class MyComponent extends React.Component {
    constructor(props) {
    super(props);
    
    this.state = {
    record: [],
    };
    }
    
    render() {
    return <div>{this.props.inputValue}</div>;
    }
    }
    
  • Question 349

    How you use decorators in React?

    You can decorate your class components, which is the same as passing the component into a function. Decorators are flexible and readable way of modifying component functionality.

    @setTitle("Profile")
    class Profile extends React.Component {
    //....
    }
    
    /*
    title is a string that will be set as a document title
    WrappedComponent is what our decorator will receive when
    put directly above a component class as seen in the example above
    */
    const setTitle = (title) => (WrappedComponent) => {
    return class extends React.Component {
    componentDidMount() {
    document.title = title;
    }
    
    render() {
    return <WrappedComponent {...this.props} />;
    }
    };
    };
    

    Note: Decorators are a feature that didn't make it into ES7, but are currently a stage 2 proposal.

  • Question 350

    What is CRA and its benefits?

    The create-react-app CLI tool allows you to quickly create & run React applications with no configuration step.

    Let's create Todo App using CRA:

    # Installation
    $ npm install -g create-react-app
    
    # Create new project
    $ create-react-app todo-app
    $ cd todo-app
    
    # Build, test and run
    $ npm run build
    $ npm run test
    $ npm start
    

    It includes everything we need to build a React app:

    1. React, JSX, ES6, and Flow syntax support.
    2. Language extras beyond ES6 like the object spread operator.
    3. Autoprefixed CSS, so you don’t need -webkit- or other prefixes.
    4. A fast interactive unit test runner with built-in support for coverage reporting.
    5. A live development server that warns about common mistakes.
    6. A build script to bundle JS, CSS, and images for production, with hashes and sourcemaps.
  • Question 351

    What is the lifecycle methods order in mounting?

    The lifecycle methods are called in the following order when an instance of a component is being created and inserted into the DOM.

    1. constructor()
    2. static getDerivedStateFromProps()
    3. render()
    4. componentDidMount()
  • Question 352

    What are the lifecycle methods going to be deprecated in React v16?

    The following lifecycle methods going to be unsafe coding practices and will be more problematic with async rendering.

    1. componentWillMount()
    2. componentWillReceiveProps()
    3. componentWillUpdate()

    Starting with React v16.3 these methods are aliased with UNSAFE_ prefix, and the unprefixed version will be removed in React v17.

  • Question 353

    What is the purpose of `getDerivedStateFromProps()` lifecycle method?

    The new static getDerivedStateFromProps() lifecycle method is invoked after a component is instantiated as well as before it is re-rendered. It can return an object to update state, or null to indicate that the new props do not require any state updates.

    class MyComponent extends React.Component {
    static getDerivedStateFromProps(props, state) {
    // ...
    }
    }
    

    This lifecycle method along with componentDidUpdate() covers all the use cases of componentWillReceiveProps().

  • Question 354

    What is the purpose of `getSnapshotBeforeUpdate()` lifecycle method?

    The new getSnapshotBeforeUpdate() lifecycle method is called right before DOM updates. The return value from this method will be passed as the third parameter to componentDidUpdate().

    class MyComponent extends React.Component {
    getSnapshotBeforeUpdate(prevProps, prevState) {
    // ...
    }
    }
    

    This lifecycle method along with componentDidUpdate() covers all the use cases of componentWillUpdate().

  • Question 355

    What is the recommended way for naming components?

    It is recommended to name the component by reference instead of using displayName.

    Using displayName for naming component:

    export default React.createClass({
    displayName: "TodoApp",
    // ...
    });
    

    The recommended approach:

    export default class TodoApp extends React.Component {
    // ...
    }
    

    also

    const TodoApp = () => {
    //...
    };
    export default TodoApp;
    
  • Question 356

    What is the recommended ordering of methods in component class?

    Recommended ordering of methods from mounting to render stage:

    1. static methods
    2. constructor()
    3. getChildContext()
    4. componentWillMount()
    5. componentDidMount()
    6. componentWillReceiveProps()
    7. shouldComponentUpdate()
    8. componentWillUpdate()
    9. componentDidUpdate()
    10. componentWillUnmount()
    11. click handlers or event handlers like onClickSubmit() or onChangeDescription()
    12. getter methods for render like getSelectReason() or getFooterContent()
    13. optional render methods like renderNavigation() or renderProfilePicture()
    14. render()
  • Question 357

    Why we need to pass a function to setState()?

    The reason behind for this is that setState() is an asynchronous operation. React batches state changes for performance reasons, so the state may not change immediately after setState() is called. That means you should not rely on the current state when calling setState() since you can't be sure what that state will be. The solution is to pass a function to setState(), with the previous state as an argument. By doing this you can avoid issues with the user getting the old state value on access due to the asynchronous nature of setState().

    Let's say the initial count value is zero. After three consecutive increment operations, the value is going to be incremented only by one.

    // assuming this.state.count === 0
    this.setState({ count: this.state.count + 1 });
    this.setState({ count: this.state.count + 1 });
    this.setState({ count: this.state.count + 1 });
    // this.state.count === 1, not 3
    

    If we pass a function to setState(), the count gets incremented correctly.

    this.setState((prevState, props) => ({
    count: prevState.count + props.increment,
    }));
    // this.state.count === 3 as expected
    

    (OR)

  • Question 358

    Why function is preferred over object for `setState()`?

    React may batch multiple setState() calls into a single update for performance. Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.

    This counter example will fail to update as expected:

    // Wrong
    this.setState({
    counter: this.state.counter + this.props.increment,
    });
    

    The preferred approach is to call setState() with function rather than object. That function will receive the previous state as the first argument, and the props at the time the update is applied as the second argument.

    // Correct
    this.setState((prevState, props) => ({
    counter: prevState.counter + props.increment,
    }));
    
  • Question 359

    Why is `isMounted()` an anti-pattern and what is the proper solution?

    The primary use case for isMounted() is to avoid calling setState() after a component has been unmounted, because it will emit a warning.

    if (this.isMounted()) {
    this.setState({...})
    }
    

    Checking isMounted() before calling setState() does eliminate the warning, but it also defeats the purpose of the warning. Using isMounted() is a code smell because the only reason you would check is because you think you might be holding a reference after the component has unmounted.

    An optimal solution would be to find places where setState() might be called after a component has unmounted, and fix them. Such situations most commonly occur due to callbacks, when a component is waiting for some data and gets unmounted before the data arrives. Ideally, any callbacks should be canceled in componentWillUnmount(), prior to unmounting.

  • Question 360

    What is the difference between constructor and getInitialState?

    You should initialize state in the constructor when using ES6 classes, and getInitialState() method when using React.createClass().

    Using ES6 classes:

    class MyComponent extends React.Component {
    constructor(props) {
    super(props);
    this.state = {
    /* initial state */
    };
    }
    }
    

    Using React.createClass():

    const MyComponent = React.createClass({
    getInitialState() {
    return {
    /* initial state */
    };
    },
    });
    

    Note: React.createClass() is deprecated and removed in React v16. Use plain JavaScript classes instead.

Get LinkedIn Premium at Rs 399