React interview questions

Today we are gonna discussed most commonly asked react interview questions

Q1. What is React?

React (also known as React.js or ReactJS) is a JavaScript library for building user interfaces. It uses a declarative paradigm that makes it easier to reason about your application and aims to be both efficient and flexible. Initially developed internally at Facebook, React has since been made open source where it continues to be developed and maintained by Facebook and the open source community.

Q2. What is Component?

Components are the building blocks of any React app and a typical React app will have many of these. Simply put, a component is a JavaScript class or function that optionally accepts inputs i.e. properties(props) and returns a React element that describes how a section of the UI (User Interface) should appear.

Click here to learn more about react component

Q3. What is JSX?

JSX is a syntax extension to JavaScript and comes with the full power of JavaScript. JSX produces React “elements”. You can embed any JavaScript expression in JSX by wrapping it in curly braces. After compilation, JSX expressions become regular JavaScript objects. This means that you can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions. Even Though React does not require JSX, it is the recommended way of describing our UI in React app.

Example

class App extends React.Component {
  render() {
    return(
      <div>
        <h1>{'Hello world!'}</h1>
      </div>
    )
  }
}

Q4. What is ReactDOM?

ReactDOM is a package that provides DOM specific methods. As the name implies, ReactDOM is the glue between React and the DOM. Often, we will only use it for one single thing: mounting with ReactDOM. Another useful feature of ReactDOM is ReactDOM.findDOMNode() which we can use to gain direct access to a DOM element. ReactDOM provides DOM specific methods only.

Q5. What is the difference between ReactDOM andReact?

React and ReactDOM were only recently split into two different libraries. Prior to v0.14, all ReactDOM functionality was part of React. This may be a source of confusion, since any slightly dated documentation won’t mention the React / ReactDOM distinction.

ReactDOM is a package that provides DOM specific methods. As the name implies, ReactDOM is the glue between React and the DOM. Often, you will only use it for one single thing: mounting with ReactDOM.render(). Another useful feature of ReactDOM is ReactDOM.findDOMNode() which you can use to gain direct access to a DOM element. (Something you should use sparingly in React apps, but it can be necessary.) If your app is “isomorphic”, you would also use ReactDOM.renderToString() in your back-end code.

For everything else, there’s React. You use React to define and create your elements, for lifecycle hooks, etc. i.e. the guts of a React application.

The reason React and ReactDOM were split into two libraries was due to the arrival of React Native. React contains functionality utilised in web and mobile apps. ReactDOM functionality is utilised only in web apps.

Q6. What is the stateless component?

stateless component is just a plain JavaScript function which takes props as an argument and returns a react element. The functional component is also known as function components

A stateless component has no state, it means that you can’t reach `this.state` inside it. It also has no lifecycle so you can’t use componentDidMount and other hooks.

Example

function Greeting(props) {
  return <h1>Hello {props.name}!</h1>;
}

Q7. What is the State?

The heart of every React component is its “state”, an object that determines how that component renders & behaves. In other words, State is a JavaScript object that stores a component’s dynamic data and determines the component’s behaviour. Because state is dynamic, it enables a component to keep track of changing information in between renders and for it to be dynamic and interactive.

State can only be used within a class component. If you anticipate that a component will need to manage state, it should be created as a class component and not a functional one.

Q8. What is the Virtual DOM?

The virtual DOM (VDOM) is a programming concept where an ideal, or “virtual”, representation of a UI is kept in memory and synced with the “real” DOM by a library such as ReactDOM. This process is called reconciliation.

Like the actual DOM, the Virtual DOM is a node tree that lists elements and their attributes and content as objects and properties. React’s render() method creates a node tree from React components and updates this tree in response to mutations in the data model, caused by actions.

Each time the underlying data changes in a React app, a new Virtual DOM representation of the user interface is created

This is where things get interesting. Updating the browser’s DOM is a three-step process in React.

  • Whenever anything may have changed, the entire UI will be re-rendered in a Virtual DOM representation.
  • The difference between the previous Virtual DOM representation and the new one will be calculated.
  • The real DOM will be updated with what has actually changed. This is very much like applying a patch.

Q9. What is the use of refs?

Refs make it possible to access DOM nodes directly within React. This comes in handy in situations where, just as one example, you want to change the child of a component. Let’s say you want to change the value of an <input> element, but without using props or re-rendering the whole component.

Q10. What is the Redux?

Redux is an open-source JavaScript library for managing application state. It is most commonly used with libraries such as React or Angular for building user interfaces. Similar to (and inspired by) Facebook’s Flux architecture, it was created by Dan Abramov and Andrew Clark

Redux makes it easy to manage the state of your application. Another way of looking at this – it helps you manage the data you display and how you respond to user actions.

The basic idea of Redux is that the entire application state is kept in a single store. The store is simply a javascript object. The only way to change the state is by firing actions from your application and then writing reducers for these actions that modify the state. The entire state transition is kept inside reducers and should not have any side-effects.

Q11. What are the controlled component ?

In HTML, form elements such as <input><textarea>, and <select>typically maintain their own state and update it based on user input. When a user submits a form the values from the aforementioned elements are sent with the form. With React it works differently. The component containing the form will keep track of the value of the input in it’s state and will re-render the component each time the callback function e.g. onChange is fired as the state will be updated. A form element whose value is controlled by React in this way is called a “controlled component”.

With a controlled component, every state mutation will have an associated handler function. This makes it straightforward to modify or validate user input.

Q12. What is the HOC?

A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API. They are a pattern that emerges from React’s compositional nature.

A higher-order component is a function that takes a component and returns a new component.

HOC’s allow you to reuse code, logic and bootstrap abstraction. HOCs are common in third-party React libraries. The most common is probably Redux’s connect function. Beyond simply sharing utility libraries and simple composition, HOCs are the best way to share behavior between React Components. If you find yourself writing a lot of code in different places that does the same thing, you may be able to refactor that code into a reusable HOC.

Q13. What is the Component Lifecycle?

React component lifecycle is a serial of methods that are automatically called by react at certain points during. This method are an optional methods. We can optionally define inside our class-based components.

Basically all the React components lifecycle methods has split in four phases: initialization, mounting, updating and unmounting.

The following are the lifecycle methods:

  • componentWillMount()
  • componentDidMount()
  • componentWillRecieveProps()
  • shouldComponentUpdate()
  • componentWillUpdate()
  • componentDidUpdate()
  • componentWillUnmount()

Click here to learn more about lifecycle

If you have found this article useful, share this article with your friends and leave comment if you have any question. Your responses are also highly appreciated.

16 Comments

  1. Odeyale Kehinde Musiliudeen said:

    This is a great job!

    August 21, 2019
    Reply
  2. ed pills online said:

    Thanks for your own work on this website. My mum takes pleasure in setting aside time for research and it is simple to grasp why. We notice all relating to the powerful way you provide very important tips and hints via your website and therefore strongly encourage response from visitors on this subject matter while our own simple princess is without a doubt understanding so much. Take pleasure in the rest of the year. Your performing a useful job. https://hierectiledysfunctionout.com/

    January 7, 2021
    Reply
  3. viagra no rx said:

    I intended to write you this very small word to help thank you very much yet again with your unique tricks you have shown on this website. This is so wonderfully open-handed with you to supply without restraint what exactly some people would have supplied for an electronic book to make some profit on their own, specifically since you might well have tried it in the event you wanted. Those tips likewise worked to provide a great way to understand that the rest have the same zeal the same as my very own to understand way more with respect to this problem. I think there are thousands of more pleasurable times up front for those who take a look at your website. https://gemerekliler.com/

    January 7, 2021
    Reply
  4. quetiapine without a prescription said:

    I’m commenting to let you be aware of of the awesome discovery my child developed browsing the blog. She came to find a good number of details, including how it is like to have a great coaching character to get a number of people clearly completely grasp a number of advanced issues. You actually did more than her desires. I appreciate you for churning out such beneficial, dependable, edifying and cool guidance on that topic to Emily. https://seroquelquetiapine.com/#

    January 9, 2021
    Reply
  5. tofranil said:

    My husband and i felt now excited Emmanuel could conclude his inquiry from your ideas he gained in your site. It’s not at all simplistic just to choose to be giving for free ideas that most people have been making money from. And we all do understand we have the website owner to be grateful to because of that. The explanations you’ve made, the easy site menu, the friendships you can give support to engender – it’s everything extraordinary, and it’s facilitating our son and our family feel that that idea is cool, and that is highly important. Many thanks for all the pieces! https://tofranilimipramine.com/#

    January 10, 2021
    Reply
  6. doxepin online said:

    I simply wanted to thank you very much once more. I do not know what I would have tried without these techniques shared by you regarding that subject matter. It was before an absolute scary scenario in my position, nevertheless taking note of this specialized tactic you solved the issue took me to weep with happiness. Now i’m thankful for your support and thus trust you really know what an amazing job you were putting in training the mediocre ones via your webpage. Most probably you haven’t met any of us. https://sinequandoxepin.com/#

    January 10, 2021
    Reply
  7. riser pci-express said:

    I simply needed to thank you so much once again. I am not sure the things that I would’ve made to happen without the type of smart ideas revealed by you over such a situation. It seemed to be a very hard issue in my opinion, however , seeing the very specialised strategy you resolved that made me to leap over gladness. I am just happy for this information and then pray you realize what a great job you happen to be providing teaching people today thru your web page. More than likely you have never got to know any of us. https://www.olx.pl/oferta/riser-008c-riser-usb3-0-pci-e-pci-1x-16x-6pin-sata-faktura-23-CID99-IDI5yoN.html

    January 16, 2021
    Reply
  8. Troy said:

    Hi to every one, it’s actually a pleasant for me
    to pay a visit this web site, it contains valuable Information.

    my web blog – cheap flights

    January 31, 2021
    Reply
  9. Cedric said:

    Every weekend i used to go to see this site, as i want enjoyment,
    for the reason that this this web site conations really nice funny
    information too.

    Also visit my web-site – cheap flights

    February 1, 2021
    Reply
  10. Mason said:

    Hello it’s me, I am also visiting this site daily, this web site is truly fastidious and the viewers are genuinely sharing nice thoughts.

    Here is my web blog – cheap flights

    February 1, 2021
    Reply
  11. Luther said:

    Hmm it seems like your blog ate my first comment (it
    was super long) so I guess I’ll just sum it up what I had written and say,
    I’m thoroughly enjoying your blog. I too am an aspiring blog writer but I’m still new to the
    whole thing. Do you have any tips for newbie blog writers?
    I’d definitely appreciate it.

    Here is my web blog :: cheap flights (tinyurl.com)

    February 3, 2021
    Reply
  12. Lucinda said:

    It’s fantastic that you are getting thoughts from this paragraph as well as from our dialogue made at this time.

    My site … cheap flights

    February 3, 2021
    Reply
  13. Lin said:

    Way cool! Some extremely valid points! I appreciate you penning this write-up plus the rest
    of the site is also really good.

    Feel free to surf to my web blog cheap flights (tinyurl.com)

    February 3, 2021
    Reply
  14. Brittney said:

    Helpful info. Fortunate me I discovered your web site by chance, and I’m shocked why this
    accident did not took place earlier! I bookmarked it.

    Here is my webpage … cheap flights; tinyurl.com,

    February 12, 2021
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *