GithubHelp home page GithubHelp logo

reactjs-interview-questions-1's Introduction

ReactJs Interview Questions

ReactJs Interview Questions

  1. What is Virtual DOM? List down the steps on how does React Handle change to the DOM?

A virtual DOM object is a representation of a DOM object, like a lightweight copy. When you render a JSX element, every single virtual DOM object gets updated. Once the virtual DOM has updated, then React compares the virtual DOM with a virtual DOM snapshot that was taken right before the update. Once React knows which virtual DOM objects have changed, then React updates those objects, and only those objects, on the real DOM. In summary, here's what happens when you try to update the DOM in React:

  • The entire virtual DOM gets updated.
  • The virtual DOM gets compared to what it looked like before you updated it. React figures out which objects have changed.
  • The changed objects, and the changed objects only, get updated on the real DOM.
  • Changes on the real DOM cause the screen to change.
  1. Why can't we call this.setState from render function of React Component?

this.setState updates the state of the component and since the component state is updated, it calls render method on its own. So, if we call this.setState from render method there will be infinite loop.

  1. What do you mean by Automatic Binding of context in React?

Automatic binding allows you to pass functions as props, and any this values in the functions' bodies will automatically refer to whatever they referred to when the function was defined. Consider following code example:

var Parent = React.createClass({
    inputChanged: function() {
        //do something using context this
    },
    render: function() {
        return <Child onChange={this.inputChanged}>
    }
})

var Child = React.createClass({
    render: function() {
        return <input onChange={this.props.onChange}>
    }
});

When input changes in Child component, inputChanged function of Parent component is called. Considering dynamic context binding in Javascript, inputChanged must have this referring to Child component, but because of automatic binding by ReactJS, the context refers to where the function is defined hence this in inputChanged refers to Parent component.

  1. What is Stateless Functional Component and how do we define them?

Component written directly as function is called stateless functional component. e.g.

function Card(props) {
    return (
        <div>
            <div>{props.name}</div>
            <div>{props.email}</div>
            <div>{props.phone}</div>
        </div>
    )
} 

When a component class has nothing else but only render function and returns JSX then stateless functional component can be used. The props parameter passed to the function is equivalent to {this.props}.

  1. What are propTypes and how do we define propTypes for Stateless Functional Component?

If a component class expects the prop to be passed from parent component then propTypes is to be used. It helps in mainly in two purposes:

  • Validation w.r.t. datatype, required etc
  • Documenting the expected properties.

propTypes in components:

var Child = React.createClass({
    propTypes: {
        name: React.PropTypes.string
    },
    render: function() {
        return <div>{this.props.name}</div>
    }
})

propTypes in stateless functional component should be provided as direct function properties:

function Child(props){
    return <div>{this.props.name}</div>
}
Child.propTypes = {
    name: React.PropTypes.string
};

Contd...

reactjs-interview-questions-1's People

Contributors

pankajkumar005 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.