React Js Interview Questions
React JS Interview Questions and Answers
1. What is React?
- React is a front-end JavaScript library developed by Facebook in 2011.
- It follows the component based approach which helps in building reusable UI components.
- It is used for developing complex and interactive web and mobile UI.
- Even though it was open-sourced only in 2015, it has one of the largest communities supporting it.
2. What are the features of React?
Major features of React are listed below:
- It uses the virtual DOM instead of the real DOM.
- It uses server-side rendering.
- It follows uni-directional data flow or data binding.
3. List some of the major advantages of React.
Some of the major advantages of React are:
- It increases the application’s performance
- It can be conveniently used on the client as well as server side
- Because of JSX, code’s readability increases
- React is easy to integrate with other frameworks like Meteor, Angular, etc
- Using React, writing UI test cases become extremely easy
Limitations of React are listed below:
- React is just a library, not a full-blown framework
- Its library is very large and takes time to understand
- It can be little difficult for the novice programmers to understand
- Coding gets complex as it uses inline templating and JSX
5. What is JSX?
return( </div> );7. Explain the purpose of render() in React.
8. How can you embed two or more components into one?
class MyComponent extends React.Component{ render(){ return( <div> <h1>Hello</h1> <Header/> </div> ); }}class Header extends React.Component{ render(){ return<h1>Header Component</h1> };}ReactDOM.render( <MyComponent/>, document.getElementById('content'));
9. What is Props?
Props is the shorthand for Properties in React. They are read-only components which must be kept pure i.e. immutable. They are always passed down from the parent to the child components throughout the application. A child component can never send a prop back to the parent component. This help in maintaining the unidirectional data flow and are generally used to render the dynamically generated data.
10. What is a state in React and how is it used?
States are the heart of React components. States are the source of data and must be kept as simple as possible. Basically, states are the objects which determine components rendering and behavior. They are mutable unlike the props and create dynamic and interactive components. They are accessed via this.state().
11. Differentiate between states and props.
11. How can you update the state of a component?
State of a component can be updated using this.setState().
class MyComponent extends React.Component { constructor() { super(); this.state = { name: 'Maxx', id: '101' } } render(){ setTimeout(()=>{this.setState({name:'Jaeha', id:'222'})},2000) return ( <div> <h1>Hello {this.state.name}</h1> <h2>Your Id is {this.state.id}</h2> </div> ); } }ReactDOM.render( <MyComponent/>, document.getElementById('content'));
12. What is arrow function in React? How is it used?Arrow functions are more of brief syntax for writing the function expression. They are also called ‘fat arrow‘ (=>) the functions. These functions allow to bind the context of the components properly since in ES6 auto binding is not available by default. Arrow functions are mostly useful while working with the higher order functions.
//General wayrender() { return( <MyInput onChange={this.handleChange.bind(this) } /> );}
//With Arrow Functionrender() { return( <MyInput onChange={ (e) => this.handleOnChange(e) } /> );}
13. Differentiate between stateful and stateless components.
13. What are the different phases of React component’s lifecycle?
There are three different phases of React component’s lifecycle:
- Initial Rendering Phase: This is the phase when the component is about to start its life journey and make its way to the DOM.
- Updating Phase: Once the component gets added to the DOM, it can potentially update and re-render only when a prop or state change occurs. That happens only in this phase.
- Unmounting Phase: This is the final phase of a component’s life cycle in which the component is destroyed and removed from the DOM.
14. Explain the lifecycle methods of React components in detail.
Some of the most important lifecycle methods are:
- componentWillMount() – Executed just before rendering takes place both on the client as well as server-side.
- componentDidMount() – Executed on the client side only after the first render.
- componentWillReceiveProps() – Invoked as soon as the props are received from the parent class and before another render is called.
- shouldComponentUpdate() – Returns true or false value based on certain conditions. If you want your component to update, return true else return false. By default, it returns false.
- componentWillUpdate() – Called just before rendering takes place in the DOM.
- componentDidUpdate() – Called immediately after rendering takes place.
- componentWillUnmount() – Called after the component is unmounted from the DOM. It is used to clear up the memory spaces.
15. What is an event in React?
In React, events are the triggered reactions to specific actions like mouse hover, mouse click, key press, etc. Handling these events are similar to handling events in DOM elements. But there are some syntactical differences like:
- Events are named using camel case instead of just using the lowercase.
- Events are passed as functions instead of strings.
The event argument contains a set of properties, which are specific to an event. Each event type contains its own properties and behavior which can be accessed via its event handler only.
16. How do you create an event in React?
class Display extends React.Component({ show(evt) { // code }, render() { // Render the div with an onClick prop (value is a function) return ( <div onClick={this.show}>Click Me!</div> ); }});
17. What are synthetic events in React?Synthetic events are the objects which act as a cross-browser wrapper around the browser’s native event. They combine the behavior of different browsers into one API. This is done to make sure that the events show consistent properties across different browsers.
18. What do you understand by refs in React?
Refs is the short hand for References in React. It is an attribute which helps to store a reference to a particular React element or component, which will be returned by the components render configuration function. It is used to return references to a particular element or component returned by render(). They come in handy when we need DOM measurements or to add methods to the components.
19. List some of the cases when you should use Refs.
Following are the cases when refs should be used:
- When you need to manage focus, select text or media playback
- To trigger imperative animations
- Integrate with third-party DOM libraries
20. How do you modularize code in React?
We can modularize code by using the export and import properties. They help in writing the components separately in different files.
//ChildComponent.jsxexport default class ChildComponent extends React.Component { render() { return( <div> <h1>This is a child component</h1> </div> ); }} //ParentComponent.jsximport ChildComponent from './childcomponent.js';class ParentComponent extends React.Component { render() { return( <div> <App /> </div> ); }
}
21. How are forms created in React?
React forms are similar to HTML forms. But in React, the state is contained in the state property of the component and is only updated via setState(). Thus the elements can’t directly update their state and their submission is handled by a JavaScript function. This function has full access to the data that is entered by the user into a form.
handleSubmit(event) { alert('A name was submitted: ' + this.state.value); event.preventDefault();} render() { return ( <form onSubmit={this.handleSubmit}> <label> Name: <input type="text" value={this.state.value} onChange={this.handleSubmit} /> </label> <input type="submit" value="Submit" /> </form> );
}
22. What do you know about controlled and uncontrolled components?
Controlled Components Uncontrolled Components 1. They do not maintain their own state 1. They maintain their own state 2. Data is controlled by the parent component 2. Data is controlled by the DOM 3. They take in the current values through props and then notify the changes via callbacks 3. Refs are used to get their current values
Stateful Component Stateless Component 1. Stores info about component’s state change in memory 1. Calculates the internal state of the components 2. Have authority to change state 2. Do not have the authority to change state 3. Contains the knowledge of past, current and possible future changes in state 3. Contains no knowledge of past, current and possible future state changes 4. Stateless components notify them about the requirement of the state change, then they send down the props to them. 4. They receive the props from the Stateful components and treat them as callback functions.
Conditions State Props 1. Receive initial value from parent component Yes Yes 2. Parent component can change value No Yes 3. Set default values inside component Yes Yes 4. Changes inside component Yes No 5. Set initial value for child components Yes Yes 6. Changes inside child components No Yes
Comments
Post a Comment