React Native Interview Questions


React native is JavaScript based on the mobile application framework. It is designed to create mobile apps for Android and iOS. Also, it does it by providing coders with a tool to use React along with the native smartphone platform. Generally, the primary benefit of the framework is that the code can be written once and shared between both OS. Moreover, JavaScript builds mobile applications that are truly native regarding look and feel. Developers are more likely to secure a high-paying job if they are experienced in React Native. To find the best talent, employers ask several React Native interview questions, which you can easily answer by reading this article.

React native is a free and open-source framework developed by Facebook. It helps developers to build cross-platform mobile applications with JavaScript. With this platform, one can easily build a mobile app using the same design principles used to develop the web application with the ReactJs framework. Developers will also get the ability to build a mobile app UI by composing several components in the declarative method. Before the react native, other alternatives such as Cordova, ionic accessible to build cross-platform applications existed.

Know More About React Native

The reason why React Native is popular is that it provides several benefits to the people using it. With this, the developer will not have to learn a new programming language to make an android application. Similarly, the developer will not need to be proficiently in objective C to make iOS applications. JavaScript is the best option when it comes to developing a mobile application for both iOS and Android. However, having Java or Swift knowledge allows developers to write performance-driven applications.

Generally, there are several benefits, such as:

  • Cross-Platform: it provides the facility to Write once and use it everywhere. Also, you can use React Native to build Windows, iOS, and Android applications.
  • Hot Reloading: Make changes in the application’s code immediately visible during development. If you change the logic of a business, the reflection will be the live loading screen.
  • Community: This framework has a large community of developers who can help you in case you need professional guidance to fix bugs and other issues that may occur.
  • Faster Development: React native assists in making fast applications. It works on the principle of the common language to build an application for Android, Windows, and iOS. It offers quicker app development, delivery, and faster marketing time.
  • Performance: The React Native code is compiled into the native code, which allows it to operate on all operating systems to give a closer native look and functions in the same manner.

Let us look at the top React Native interview questions and answers for beginners.

What is React?

React is a declarative, efficient, flexible open source front-end JavaScript library developed by Facebook in 2011. It follows the component-based approach for building reusable UI components, especially for single page applications. Also, it is used for developing interactive view layers of web and mobile apps. It was created by Jordan Walke, a software engineer at Facebook. Moreover, you should know that it was initially deployed on Facebook’s News Feed section in 2011 and later used in its products like WhatsApp & Instagram.

What are the features of React?

React framework is gaining quick popularity as the best framework among web developers. The main features of React are:

  • JSX
  • Components
  • One-way Data Binding
  • Virtual DOM
  • Simplicity
  • Performance

What are the most crucial advantages of using React?

Following is a list of the most crucial advantages of using React:

React is easy to learn and use

React comes with good availability of documentation, tutorials, and training resources. It is easy for any developer to switch from JavaScript background to React and easily understand and start creating web apps using React. Also, anyone with little knowledge of JavaScript can start building web applications using React.

React follows the MVC architecture.

React is the V (view part) in the MVC (Model-View-Controller) architecture model and is referred to as “one of the JavaScript frameworks.” It is not fully featured but has many advantages of the open-source JavaScript User Interface (UI) library, which also helps execute the task in a better manner.

React uses Virtual DOM to improve efficiency.

React uses virtual DOM to render the view. The virtual DOM is a virtual representation of the real DOM. Each time the data changes in a react app, a new virtual DOM gets created. Creating a virtual DOM is much faster than rendering the UI inside the browser. Therefore, with the use of virtual DOM, the efficiency of the app improves. That’s why React provides great efficiency.

What are the most crucial advantages of using React?

Following is a list of the most crucial advantages of using React:

React is easy to learn and use

React comes with good availability of documentation, tutorials, and training resources. It is easy for any developer to switch from JavaScript background to React and easily understand and start creating web apps using React. Anyone with little knowledge of JavaScript can start building web applications using React.

React follows the MVC architecture.

React is the V (view part) in the MVC (Model-View-Controller) architecture model and is referred to as “one of the JavaScript frameworks.” It is not fully featured but has many advantages of the open-source JavaScript User Interface (UI) library, which also helps execute the task in a better manner.

React uses Virtual DOM to improve efficiency.

React uses virtual DOM to render the view. The virtual DOM is a virtual representation of the real DOM. Each time the data changes in a react app, a new virtual DOM gets created. Creating a virtual DOM is much faster than rendering the UI inside the browser. Therefore, with the use of virtual DOM, the efficiency of the app improves. That’s why React provides great efficiency.

React has a rich set of libraries.

React has a huge ecosystem of libraries and provides you the freedom to choose the tools, libraries, and architecture for developing the best application based on your requirement.

What are the biggest limitations of React?

Following is the list of the biggest limitations of React:

  • React is just a library. It is not a complete framework.
  • It has a huge library which takes time to understand.
  • Also, it may be difficult for the new programmers to understand and code.
  • React uses inline templating and JSX, which may be difficult and act as a barrier. It also makes the coding complex.

What is FlexBox in React Native?

Flexbox in React Native works in the same way as it does in CSS on the web, with a few exceptions. It is generally used to provide a consistent layout on different screen sizes.

What is the use of FlatList in React Native?

FlatList in React Native is a component that displays the content in similarly structured data as a scrollable list. It is used for large lists of data where the number of list items changes over time.

How to get Geolocation in React Native?

The geolocation API extends the web spec of the geolocation. In android, the Geolocation uses android.location.API.

The following alternative libraries are used to include the new Location Services API with the React Native.

React-native-geolocation-service

React-native-location

To request location access, the following code needs to be included inside the application’s AndroidManifest.xml file.

<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/>

What is JSX?

JSX stands for JavaScript XML. It is a React extension which allows writing JavaScript code that looks similar to HTML. It makes HTML files easy to understand. Moreover, the JSX file makes the React application robust and boosts its performance. JSX allows you to write XML-like syntax in the same file where you write JavaScript code, and then preprocessors (i.e., transpilers like Babel) transform these expressions into actual JavaScript code. Just like XML/HTML, JSX tags have a tag name, attributes, and children.

Example

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

In the above example, text inside <h1> tag returns as JavaScript function to the render function. After compilation, the JSX expression becomes a normal JavaScript function, as shown below.

React.createElement("h1", null, "Hello JavaTpoint");

Why can’t browsers read JSX?

Browsers cannot read JSX directly because they can only understand JavaScript objects, and JSX is not a regular JavaScript object. Thus, we need to transform the JSX file into a JavaScript object using transpilers like Babel and then pass it to the browser.

Why do we use JSX?

  • It is faster than regular JavaScript because it performs optimization while translating the code to JavaScript.
  • Instead of separating technologies by putting markup and logic in separate files, React uses components that contain both.
  • Also, it is type-safe, and most of the errors can be found at compilation time.
  • It makes it easier to create templates.

What does StyleSheet.create do in React Native?

StyleSheet.create method ensures that values are immutable and opaque, they are also only created once.

What do you understand about Virtual DOM?

A Virtual DOM is a lightweight JavaScript object which is an in-memory representation of a real DOM. It is an intermediary step between the render function being called and the displaying of elements on the screen. It is similar to a node tree which lists the elements, their attributes, and content as objects and their properties. The render function creates also a node tree of the React components and then updates this node tree in response to the mutations in the data model caused by various actions done by the user or by the system.

How is React different from Angular?

The React is different from Angular in the following ways.

AngularReact
AuthorGoogleFacebook Community
DeveloperMisko HeveryJordan Walke
Initial ReleaseOctober 2010March 2013
LanguageJavaScript, HTMLJSX
TypeOpen Source MVC FrameworkOpen Source JS Framework
RenderingClient-SideServer-Side
Data-BindingBi-directionalUni-directional
DOMRegular DOMVirtual DOM
TestingUnit and Integration TestingUnit Testing
App ArchitectureMVCFlux
PerformanceSlowFast, due to virtual DOM.

Can we combine native iOS or Android code in React Native.

Yes, we can. React Native smoothly combines the components written in Objective-C, Java, or Swift.

How React’s ES6 syntax is different from ES5 syntax?

The React’s ES6 syntax has changed from ES5 syntax in the following aspects.

require vs. Import

// ES5  
var React = require('react');  
   
// ES6  
import React from 'react';

exports vs. export

// ES5  
module.exports = Component;  
   
// ES6  
export default Component; 
 

component and function

// ES5  
var MyComponent = React.createClass({  
    render: function() {  
        return(  
          <h3>Hello JavaTpoint</h3>  
        );  
    }  
});  
   
// ES6  
class MyComponent extends React.Component {  
    render() {  
        return(  
          <h3>Hello Javatpoint</h3>  
        );  
    }  
}  

props

// ES5  
var App = React.createClass({  
    propTypes: { name: React.PropTypes.string },  
    render: function() {  
        return(  
           <h3>Hello, {this.props.name}!</h3>  
        );  
    }  
});  
   
// ES6  
class App extends React.Component {  
    render() {  
        return(  
          <h3>Hello, {this.props.name}!</h3>  
        );  
    }  
}  

state

var App = React.createClass({  
    getInitialState: function() {  
        return { name: 'world' };  
    },  
    render: function() {  
        return(  
          <h3>Hello, {this.state.name}!</h3>  
        );  
    }  
});  
   
// ES6  
class App extends React.Component {  
    constructor() {  
        super();  
        this.state = { name: 'world' };  
    }  
    render() {  
        return(  
          <h3>Hello, {this.state.name}!</h3>  
        );  
    }  
} 
 

What types of mobile apps can we create with React Native?

With react native we can generally create following types of app:

Working prototypes Apps

Apps with streamlined UI

Cross-platform apps

An app without the use of native APIs

What is the difference between ReactJS and React Native?

The main differences between ReactJS and React Native are given below.

SNReactJSReact Native
1.Initial release in 2013.Initial release in 2015.
2.It is used for developing web applications.It is used for developing mobile applications.
3.It can be executed on all platforms.It is not platform independent. It takes more effort to be executed on all platforms.
4.It uses a JavaScript library and CSS for animations.It comes with built-in animation libraries.
5.It uses React-router for navigating web pages.It has a built-in Navigator library for navigating mobile applications.
6.It uses HTML tags.It does not use HTML tags.
7.In this, the Virtual DOM renders the browser code.In this, Native uses its API to render code for mobile applications.

For what XHR Module is used in React Native?

XHR Module is used for implementation of XMLHttpRequest to post data on the server.

What is the difference between Real DOM and Virtual DOM?

The following table specifies the key differences between the Real DOM and Virtual DOM:

The real DOM creates a new DOM if the element updates.

Real DOMVirtual DOM
The real DOM updates slower.The virtual DOM updates faster.
The real DOM can directly update HTML.The virtual DOM cannot directly update HTML.
The virtual DOM updates the JSX if the element updates.
In real DOM, DOM manipulation is very expensive.In virtual DOM, DOM manipulation is very easy.
There is a lot of memory wastage in The real DOM.There is no memory wastage in the virtual DOM.

What do you understand from “In React, everything is a component.”

In React, components are the building blocks of React applications. These components divide the entire React application’s UI into small, independent, and reusable pieces of code. React renders each of these components independently without affecting the rest of the application UI. Hence, we can say that, in React, everything is a component.

Explain the purpose of render() in React.

It is mandatory for each React component to have a render() function. Render function is used to return the HTML which you want to display in a component. If you need to render more than one HTML element, you need to group together inside a single enclosing tag (parent tag) such as <div>, <form>, <group> etc. This function returns the same result each time it is invoked.

Example: If you need to display a heading, you can do this as below.

import React from 'react'  
   
class App extends React.Component {  
   render (){  
      return (  
         <h1>Hello World</h1>  
      )  
   }  
}  
export default App 
 

Points to Note:

  • Each render() function contains a return statement.
  • The return statement can have only one parent HTML tag.

How can you embed two or more components into one?

You can embed two or more components into the following way:

import React from 'react'  
   
class App extends React.Component {  
   render (){  
      return (  
         <h1>Hello World</h1>  
      )  
   }  
}  
   
class Example extends React.Component {  
   render (){  
      return (  
         <h1>Hello JavaTpoint</h1>  
      )  
   }  
}  
export default App 

What is Props?

Props stand for “Properties” in React. They are read-only inputs to components. Props are an object which stores the value of attributes of a tag and work similar to the HTML attributes. Moreover, it gives a way to pass data from the parent to the child components throughout the application.

It is similar to function arguments and passed to the component in the same way as arguments passed in a function.

Moreover, props are immutable so we cannot modify the props from inside the component. Inside the components, we can add attributes called props. These attributes are available in the component as this.props and generally you can use it to render dynamic data in our render method.

What is a State in React?

The State is an updatable structure which holds the data and information about the component. It may be changed over the lifetime of the component in response to user action or system event. It is the heart of the react component which determines the behavior of the component and how it will render. It must be kept as simple as possible.

Let’s create a “User” component with “message state.”

import React from 'react'  
  
class User extends React.Component {  
  constructor(props) {  
    super(props)  
  
    this.state = {  
      message: 'Welcome to JavaTpoint'  
    }  
  }  
  
  render() {  
    return (  
      <div>  
        <h1>{this.state.message}</h1>  
      </div>  
    )  
  }  
}  
export default User 
 

Which function is used to pass values between screens in React Native?

this.props.navigation.navigate() method is used to pass values between screens in React Native.

Syntax:

this.props.navigation.navigate(‘RouteName’, { /* params go here */ })

Differentiate between States and Props.

The major differences between States and Props are given below.

SNPropsState
1.Props are read-only.State changes can be asynchronous.
2.Props are immutable.State is mutable.
3.Props allow you to pass data from one component to other components as an argument.State holds information about the components.
4.Props can be accessed by the child component.State cannot be accessed by child components.
5.Props are used to communicate between components.States can be used for rendering dynamic changes with the component.
6.The stateless component can have Props.The stateless components cannot have State.
7.Props make components reusable.The State cannot make components reusable.
8.Props are external and controlled by whatever renders the component.The State is internal and controlled by the component itself.

Differentiate between stateless and stateful components.

The difference between stateless and stateful components are:

SNStateless ComponentStateful Component
1.The stateless components do not hold or manage state.The stateful components can hold or manage state.
2.It does not contain the knowledge of past, current, and possible future state changes.It can contain the knowledge of past, current, and possible future changes in state.
3.It is also known as a functional component.It is also known as a class component.
4.It is simple and easy to understand.It is complex as compared to the stateless component.
5.It does not work with any lifecycle method of React.It can work with all lifecycle method of React.
6.The stateless components cannot be reused.The stateful components can be reused.

What is the arrow function in React? How is it used?

The Arrow function is the new feature of the ES6 standard. If you need to use arrow functions, it is not necessary to bind any event to ‘this.’ Here, the scope of ‘this’ is global and not limited to any calling function. So If you are using Arrow Function, there is no need to bind ‘this’ inside the constructor. It is also called ‘fat arrow ‘(=>) functions.

//General way  
render() {      
    return(  
        <MyInput onChange={this.handleChange.bind(this) } />  
    );  
}  
//With Arrow Function  
render() {    
    return(  
        <MyInput onChange={ (e) => this.handleOnChange(e) } />  
    );  
}  

What is an event in React?

An event is an action which triggers as a result of the user action or system generated event like a mouse click, loading of a web page, pressing a key, window resizes, etc. In React, the event handling system is very similar to handling events in DOM elements. Also, the React event handling system is known as Synthetic Event, which is a cross-browser wrapper of the browser’s native event.

Generally, handling events with React have some syntactical differences, which are:

  • React events are named as camelCase instead of lowercase.
  • With JSX, a function is passed as the event handler instead of a string.

What is the significance of keys in React?

A key is a unique identifier. In React, you can use it to identify which items have changed, updated, or deleted from the Lists. Also, it is useful when we dynamically create components or when the users alter the lists. It also helps to determine which components in a collection need to be re-rendered instead of re-rendering the entire set of components every time. It increases application performance.

How are forms created in React?

Forms allow the users to interact with the application as well as gather information from the users. Forms can also perform many tasks such as user authentication, adding user, searching, filtering, etc. Moreover, a form can contain text fields, buttons, checkbox, radio button, etc.

React offers a stateful, reactive approach to build a form. The forms in React are similar to HTML forms. But in React, the state property of the component is only updated via setState(), and a JavaScript function handles their submission. This function has full access to the data which is entered by the user into a form.

import React, { Component } from 'react';  
  
class App extends React.Component {  
  constructor(props) {  
      super(props);  
      this.state = {value: ''};  
      this.handleChange = this.handleChange.bind(this);  
      this.handleSubmit = this.handleSubmit.bind(this);  
  }  
  handleChange(event) {  
      this.setState({value: event.target.value});  
  }  
  handleSubmit(event) {  
      alert('You have submitted the input successfully: ' + this.state.value);  
      event.preventDefault();  
  }  
  render() {  
      return (  
          <form onSubmit={this.handleSubmit}>  
            <h1>Controlled Form Example</h1>  
            <label>  
                Name:  
                <input type="text" value={this.state.value} onChange={this.handleChange} />  
            </label>  
            <input type="submit" value="Submit" />  
         </form>  
      );  
  }  
}  
export default App;  

What are the different phases of React component’s lifecycle?

The different phases of React component’s lifecycle are:

Initial Phase: It is the birth phase of the React lifecycle when the component starts its journey on a way to the DOM. In this phase, a component contains the default Props and initial State. Moreover, these default properties are done in the constructor of a component.

Mounting Phase: In this phase, the instance of a component is created and added into the DOM.

Updating Phase: It is the next phase of the React lifecycle. In this phase, we get new Props and change State. This phase can potentially update and re-render only when a prop or state change occurs. Moreover, the main aim of this phase is to ensure that the component is displaying the latest version of itself. Generally, this phase repeats again and again.

Unmounting Phase: It is the final phase of the React lifecycle, where the component instance is destroyed and unmounted(removed) from the DOM.

What are Pure Components?

Pure components introduced in React 15.3 version. The React.Component and React.PureComponent differ in the shouldComponentUpdate() React lifecycle method. This method also decides the re-rendering of the component by returning a boolean value (true or false). In React.Component, the shouldComponentUpdate() method returns true by default. But in React.PureComponent, it compares the changes in state or props to re-render the component. The pure component enhances the simplicity of the code and performance of the application.

What are Higher Order Components(HOC)?

In React, Higher Order Component is an advanced technique for reusing component logic. It is a function that takes a component and returns a new component. In other words, it is a function which accepts another function as an argument. Moreover, according to the official website, it is not the feature(part) in React API, but a pattern that emerges from React’s compositional nature.

What can you do with HOC?

Generally, you can do many tasks with HOC; some of them are:

  • Code Reusability
  • Props manipulation
  • State manipulation
  • Render highjacking

What is the use of the platform module in React Native?

The React Native Platform module is used to detect the platform of the device in which the application is running. You can also use the below code detect platform in React Native.

import {Platform, StyleSheet} from ‘react-native’;
const styles = StyleSheet.create({
height: Platform.OS === ‘ios’ ? 200 : 100,
});

What is the difference between Element and Component?

The main differences between Elements and Components are:

SNElementComponent
1.An element is a plain JavaScript object which describes the component state and DOM node, and its desired properties.A component is the core building block of React application. It is a class or function which accepts an input and returns a React element.
2.It only holds information about the component type, its properties, and any child elements inside it.It can contain state and props and has access to the React lifecycle methods.
3.It is immutable.It is mutable.
4.We cannot apply any methods on elements.We can apply methods on components.
5.Example:const element = React.createElement(‘div’,{id: ‘login-btn’},’Login’)Example:function Button ({ onLogin }) {return React.createElement(‘div’,{id: ‘login-btn’, onClick: onLogin},’Login’)}

What is a Gesture Responder System?

The gesture responder system manages the lifecycle of gestures in your app. A touch can go through several phases as the app determines what the user’s intention is. For example, the app needs to determine if the touch is scrolling, sliding on a widget, or tapping. This can even change during the duration of a touch. There can also be multiple simultaneous touches.

The touch responder system is needed to allow components to negotiate these touch interactions without any additional knowledge about their parent or child components.

How to write comments in React?

In React, we can write comments as we write comments in JavaScript. It can be in two ways:

1. Single Line Comments: We can write comments as /* Block Comments */ with curly braces:

{/* Single Line comment */}  

2. Multiline Comments: If we want to comment more that one line, we can do this as

{ /*  

   Multi 

   line 

   comment 

*/ }

Explain what is the use of webView in React Native?

WebView is a React Native is a component that is implemented to load a web page or web content. It is imported from the core of the react-native library. The WebView is replaced from the inbuilt core react native and is then placed inside the react-native webview library. In other words, WebView is a bridge that connects web platforms with the react native. It also gives the users different options to create connections to an application that is running on the web.

WebView can also control the navigation and load an application into the web page. Webview is an element of the React Native that needs no installation or configuration.

Why is it necessary to start component names with a capital letter?

In React, it is necessary to start component names with a capital letter. If we start the component name with lower case, it will throw an error as an unrecognized tag. It is because, in JSX, lower case tag names are considered as HTML tags.

Is it possible for a web browser to read JSX directly?

Web browsers can’t read JSX directly. This is because the web browsers are built to read the regular JS objects only, and JSX is not a regular JavaScript object.

If you want a web browser to read a JSX file, you must transform the files into a regular JavaScript object. For this purpose, you can also use Babel.

What is JavaScriptCore?

JavaScriptCore is an open-source Web browser engine which runs in WebKit. It helps developers to create their own browser.

What happens when you call setState?

The first thing React will do when you call setState is merge the object you passed into setState into the current state of the component. This will also kick off a process called reconciliation. Generally, the end goal of reconciliation is to update the UI based on this new state in the most efficient way possible.

To do this, React will also construct a new tree of React elements (which you can think of as an object representation of your UI). Once it has this tree, in order to figure out how the UI should change in response to the new state. Also, React will diff this new tree against the previous element tree.

Moreover, by doing this, react will then know the exact changes which occurred, and by knowing exactly what changes occurred, will be able to minimize its footprint on the UI by only making updates where absolutely necessary.

When would you use ScrollView over FlatList or vice-versa?

  • Do you need to render a list of similar items from an array or the data is very big? Use FlatList
  • Do you need to render generic content in a scrollable container and the data is small? Use ScrollView

How to use map function in React Native?

The map function is used to show a list of elements in an array. Also, you can use it in React Native like the following example:

Example

import React, { Component } from "react";
import { Text, View } from "react-native";

export default class mapFunction extends Component {
constructor(props) {
super(props);
this.state = {
array: [
{
title: "example title 1",
subtitle: "example subtitle 1"
},
{
title: "example title 2",
subtitle: "example subtitle 2"
},
{
title: "example title 3",
subtitle: "example subtitle 3"
}
]
};
}

list = () => {
return this.state.array.map(element => {
return (
<View style={{ margin: 10 }}>
<Text>{element.title}</Text>
<Text>{element.subtitle}</Text>
</View>
);
});
};

render() {
return <View>{this.list()}</View>;
}
}

How are Hot Reloading and Live Reloading in React Native different?

  • Live reloading reloads or refreshes the entire app when a file changes. For example, if you were four links deep into your navigation and saved a change, live reloading would restart the app and load the app back to the initial route.
  • Hot reloading only refreshes the files that were changed without losing the state of the app. For example, if you were four links deep into your navigation and saved a change to some styling, the state would not change, but the new styles would appear on the page without having to navigate back to the page you are on because you would still be on the same page.

How to fetch data from a local JSON file on React Native?

Option 1:

const customData = require('./customData.json');

and then access customData like a normal JS object.

Option 2:

For ES6/ES2015 you can import directly like:

// app.js
import * as data from './example.json';
const word = data.name;
console.log(word); // output 'testing'

How to use the camera in React Native?

To use the camera to React Native, follow these steps carefully:

On Android, camera permission must be asked:

<uses-permission android:name=”android.permission.CAMERA” />

Now, to enable video recording feature add the following code to the AndroidManifest.xml:

<uses-permission android:name=”android.permission.RECORD_AUDIO”/> <uses-permission android:name=”android.permission.READ_EXTERNAL_STORAGE” /> <uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE” />

For iOS, you must update the Info.plist with a usage description for camera

<key>NSCameraUsageDescription</key> <string>Your own description of the purpose</string>