Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 17, 2021 04:05 pm GMT

React interview questions and answers for JS developers

You can check all 161 React tech interview questions here

1. How does React work?

Answer:

React creates a virtual DOM. When state changes in a component it firstly runs a "diffing" algorithm, which identifies what has changed in the virtual DOM. The second step is reconciliation, where it updates the DOM with the results of diff.

Source:github.com/Pau1fitz

2. What is context?

Answer:

Context provides a way to pass data through the component tree without having to pass props down manually at every level. For example, authenticated user, locale preference, UI theme need to be accessed in the application by many components.

const {Provider, Consumer} = React.createContext(defaultValue);
Enter fullscreen mode Exit fullscreen mode

Source:github.com/sudheerj

3. What is virtual DOM?

Answer:

The virtual DOM (VDOM) is an in-memory representation of Real DOM. The representation of a UI is kept in memory and synced with the real DOM. Its a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called reconciliation.

Source:github.com/sudheerj

4. What is props in ReactJS?

Answer:

Props are inputs to a React component. They are single values or objects containing a set of values that are passed to React Components on creation using a naming convention similar to HTML-tag attributes. i.e, They are data passed down from a parent component to a child component.

The primary purpose of props in React is to provide following component functionality:

  1. Pass custom data to your React component.
  2. Trigger state changes.
  3. Use via this.props.reactProp inside component's render() method.

For example, let us create an element with reactProp property,
Alt Text
This reactProp (or whatever you came up with) name then becomes a property attached to React's native props object which originally already exists on all components created using React library.
Alt Text

Source:https://github.com/sudheerj

5. What is the use of refs?

Answer:

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

Source:github.com/sudheerj

6. What is JEST?

Answer:

Jest is a JavaScript unit testing framework made by Facebook based on Jasmine and provides automated mock creation and a jsdom environment. It's often used for testing React components.

Source:github.com/sudheerj

7. What are the advantages of ReactJS?

Answer:

Below are the advantages of ReactJS:

  1. Increases the applications performance with Virtual DOM
  2. JSX makes code is easy to read and write
  3. It renders both on client and server side
  4. Easy to integrate with other frameworks (Angular, BackboneJS) since it is only a view library
  5. Easy to write UI Test cases and integration with tools such as JEST.

Source:github.com/sudheerj

8. What is ReactJS?

Answer:

ReactJS is an open-source frontend JavaScript library which is used for building user interfaces especifically for single page applications. It is used for handling view layer for web and mobile apps. React was created by Jordan Walke, a software engineer working for Facebook. ReactJS was first deployed on Facebooks newsfeed in 2011 and on Instagram.com in 2012.

Source:https://github.com/sudheerj

9. How to write comments in ReactJS?

Answer:

The comments in ReactJS/JSX is similar to javascript multiline comments which are wrapped with curly braces:

Alt Text

Source:github.com/sudheerj

10. How would you write an inline style in React?

Answer:

For example:

Alt Text

Source:github.com/WebPredict

11. What are the major features of ReactJS?

Answer:

The major features of ReactJS are as follows,

  • It uses VirtualDOM instead RealDOM considering that RealDOM manipulations are expensive.
  • Supports server-side rendering
  • Follows Unidirectional data flow or data binding
  • Uses reusable/composable UI components to develop the view

Source:https://github.com/sudheerj

12. What are props in React?

Answer:

Props are properties that are passed into a child component from its parent, and are readonly.

Source:github.com/WebPredict

13. What are the differences between a class component and functional component?

Answer:

  • Class components allows you to use additional features such as local state and lifecycle hooks. Also, to enable your component to have direct access to your store and thus holds state.

  • When your component just receives props and renders them to the page, this is a stateless component, for which a pure function can be used. These are also called dumb components or presentational components.

Source:github.com/Pau1fitz

14. Where in a React component should you make an AJAX request?

Answer:

componentDidMount is where an AJAX request should be made in a React component.

This method will be executed when the component mounts (is added to the DOM) for the first time. This method is only executed once during the components life. Importantly, you cant guarantee the AJAX request will have resolved before the component mounts. If it doesn't, that would mean that youd be trying to setState on an unmounted component, which would not work. Making your AJAX request in componentDidMount will guarantee that theres a component to update.

Source:github.com/Pau1fitz

15. What is the difference between state and props?

Answer:

The state is a data structure that starts with a default value when a Component mounts. It may be mutated across time, mostly as a result of user events.

Props (short for properties) are a Component's configuration. They are received from above and immutable as far as the Component receiving them is concerned. A Component cannot change its props, but it is responsible for putting together the props of its child Components. Props do not have to just be data - callback functions may be passed in as props.

Source:github.com/Pau1fitz

16. What is the difference between a Presentational component and a Container component?

Answer:

  • Presentational components are concerned with how things look. They generally receive data and callbacks exclusively via props. These components rarely have their own state, but when they do it generally concerns UI state, as opposed to data state.

  • Container components are more concerned with how things work. These components provide the data and behavior to presentational or other container components. They call Flux actions and provide these as callbacks to the presentational components. They are also often stateful as they serve as data sources.

Source:github.com/Pau1fitz

17. What are refs used for in React?

Answer:

Refs are an escape hatch which allow you to get direct access to a DOM element or an instance of a component. In order to use them you add a ref attribute to your component whose value is a callback function which will receive the underlying DOM element or the mounted instance of the component as its first argument.

class UnControlledForm extends Component {  handleSubmit = () =&gt; {    console.log("Input Value: ", this.input.value);  };  render() {    return (         (this.input = input)} /&gt;        <button type="submit">Submit</button>    );  }}
Enter fullscreen mode Exit fullscreen mode

Above notice that our input field has a ref attribute whose value is a function. That function receives the actual DOM element of input which we then put on the instance in order to have access to it inside of the handleSubmit function.

Its often misconstrued that you need to use a class component in order to use refs, but refs can also be used with functional components by leveraging closures in JavaScript.

function CustomForm({ handleSubmit }) {  let inputElement;  return (     handleSubmit(inputElement.value)}&gt;       (inputElement = input)} /&gt;      <button type="submit">Submit</button>  );}
Enter fullscreen mode Exit fullscreen mode

Source:github.com/Pau1fitz

18. What's the difference between a controlled component and an uncontrolled one in React?

Answer:

  • A controlled component has its state completely driven by React,
  • Uncontrolled components can maintain their own internal state. E.g., a textarea's value.

Source:github.com/WebPredict

Thanks for reading and good luck on your next tech interview!

Explore 3800+ dev interview question here Devinterview.io


Original Link: https://dev.to/devinterview/react-interview-questions-and-answers-for-js-developers-h1m

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To