Exploring React Native: A Comparative Analysis of the DOM vs. Traditional Native Development

CodeStax.Ai
6 min readAug 20, 2024

--

React Native has become one of the most famous frameworks in the world when talking about cross-platform applications. It was adopted by thousands of businesses all over the world, including giants like Uber and Facebook. React Native enables the creation of native mobile applications on iOS and Android from one codebase. A framework that stitches the building blocks to drive a mobile app with JavaScript and React, ensuring a consistent development experience on several platforms.

Why React Native?

Cross-platform Development: It is one of the maximum advantages offered by React Native, which helps in developing apps for Android and iOS by writing code once only, usually with minor modifications. JavaScript: It is one of the most used today in mobile app development. Thus, React Native has availed itself of JavaScript to facilitate the modernization of many developers’ existing JavaScript skills and tools.

Traditional Native Development

In contrast, native development of mobile applications means a developer creating an application for each platform, be it Android or iOS, in its languages and tools. For Android, this would mostly be Java or Kotlin together with the Android Studio, generally also the officially supported Integrated Development Environment and IDE. For iOS, either Swift or Objective-C is used together with Xcode, Apple’s integrated development environment.

The Virtual DOM in React Native

React Native is a part of React, quite a well-known JavaScript library in web development. It utilizes the Virtual DOM. The Virtual DOM is a lightweight, in-memory representation of the real DOM. This helps to make React’s handling of changes to the DOM effective because it limits the manipulation of the real DOM. Dramatically, it will boost the performance of the application.

What is the Virtual DOM, and how does it work?

  1. Initial Rendering: The first thing that takes place for the whole UI is a virtual DOM in which React elements are created and then rendered.
  2. State and Props Changes: On change in states and props, React updates the Virtual DOM without immediately affecting the real DOM.
  3. Diff Algorithm: React runs a diffing algorithm for comparing the current Virtual DOM with the last version, evaluating the differences.
  4. Reconciliation Process: Keeping these differences in view, React evaluates the best way to update the real DOM and brings in changes only that are required.
  5. Real Actualization of Real DOM: The changes are made in the real DOM. It may include any addition, deletion, or update that is necessary.

React vs. React Native

Whereas it is based on some of the central ideas of React itself, React Native takes React into mobile development. This allows you to create apps for both iOS and Android using React’s component-based architecture.

Component-Based Architecture

Core to the popularity of React is its component-based architecture. It essentially contains the building blocks for your mobile application in React Native and encapsulates part of the user interface, letting you construct complicated interfaces out of simple ones. Components in React Native could be:

  1. Functional Components: JavaScript functions take arguments and return React elements. They are simpler and easier to read and test.
import React, { useState } from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
const FunctionalComponent = () => {
const [count, setCount] = useState(0);
return (
<View style={styles.container}>
<Text style={styles.text}>Count: {count}</Text>
<Button title="Increment" onPress={() => setCount(count +
1)} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 20,
marginBottom: 10,
},
});
export default FunctionalComponent;

2. Class Components: These are the traditional components that extend React.Component, and they have more features provided out of the box, such as lifecycle methods.

import React, { Component } from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
class ClassComponent extends Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
}
increment = () => {
this.setState(prevState => ({ count: prevState.count + 1 }));
};
render() {
return (
<View style={styles.container}>
<Text style={styles.text}>Count:
{this.state.count}</Text>
<Button title="Increment" onPress={this.increment} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 20,
marginBottom: 10,
},
});
export default ClassComponent;

React component lifecycle and state management

Proper management of the component lifecycle and state is necessary for efficiently building a React application. The component lifecycle methods are shown below:

  1. Constructor: This is the method that is used in setting up a component’s initial state and in binding event handlers. It is part of ES6 classes.
  2. ComponentDidMount: Called after the component is mounted, and used for data fetching or setup.
  3. ComponentDidUpdate: The method is called after a component has been updated. This lets one do further rendering or trigger side effects.
  4. ComponentWillUnmount: Called before the component is removed, for cleanups.
  5. State Management: An improvement in the internal state of the component; this state can be pictured in the form of a JavaScript object. It’s modified by applying the setState method. After these state changes, React re-renders the components automatically.
import React, { Component } from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
class LifecycleComponent extends Component {
constructor(props) {
super(props);
this.state = {
count: 0,
};
console.log('Constructor: Component is being constructed');
}
componentDidMount() {
console.log('componentDidMount: Component has been mounted');
// Fetch data or perform side effects here
}
componentDidUpdate(prevProps, prevState) {
console.log('componentDidUpdate: Component has been updated');
// Perform actions in response to state or prop changes
if (prevState.count !== this.state.count) {
console.log('Count has changed:', this.state.count);
}
}
componentWillUnmount() {
console.log('componentWillUnmount: Component is about to be
unmounted');
// Cleanup activities, such as invalidating timers or canceling
network requests
}
increment = () => {
this.setState(prevState => ({ count: prevState.count + 1 }));
};
render() {
console.log('render: Component is being rendered');
return (
<View style={styles.container}>
<Text style={styles.text}>Count: {this.state.count}</Text>
<Button title="Increment" onPress={this.increment} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 20,
marginBottom: 10,
},
});
export default LifecycleComponent;

Component Communication

For a proper flow of the project, it is very important that your components communicate well. As mentioned, common patterns include:

  1. Parent-to-Child Communication: Props are how one passes data from a parent down into a child component.
  2. Child to Parent Communication: Props can also be used to pass functions to child components, and then have them invoked in the context of the parent.

Notice that communication between components in React Native is not similar to traditional JavaScript events. There is no event bubbling.

Pros of React Native

  1. Versatility: React Native provides support for native components written in Objective-C, Java, or Swift, facilitating the incorporation of existing code.
  2. Large Developer Community: Being open-source, it has an active and vibrant community that contributed a lot and provided support.
  3. Faster Development: It allows development to be faster compared to native technologies.
  4. Great Performance: The tests comparing React Native and native apps indicate that it performs nearly at native app speed; however, one needs to optimise the JavaScript code to achieve near-native performance.

Cons of React Native

  1. Possible Incompatible and Debugging Issues: Though it has wide adoption, React Native itself is still in development and might have either compatibility or debugging issues since it stays in beta and has a huge number of community packages.
  2. Not All Custom Modules Are Present: Several components will either not be available or they will need to be rewritten from scratch, thus there will be dissimilarities between the two platforms.
  3. Upgrading Versions: Moving into new versions of React Native has different issues, but upgrade helpers and guides from the community greatly help make transitions easier.

About the Author

Vithyaghar is a Software Development Engineer with a strong passion for both front-end and back-end technologies. Skilled in front-end frameworks like React Native and Vue.js and back-end development with Node.js, Vithyaghar enjoys creating seamless, user-friendly applications and building robust, efficient systems that meet modern software demands.

About CodeStax.Ai

At CodeStax.AI, we stand at the nexus of innovation and enterprise solutions, offering technology partnerships that empower businesses to drive efficiency, innovation, and growth, harnessing the transformative power of no-code platforms and advanced AI integrations.

But what is the real magic? It’s our tech tribe behind the scenes. If you have a knack for innovation and a passion for redefining the norm, we have the perfect tech playground for you. CodeStax. Ai offers more than a job — it’s a journey into the very heart of what’s next. Join us and be part of the revolution that’s redefining the enterprise tech landscape.

--

--

CodeStax.Ai
CodeStax.Ai

Written by CodeStax.Ai

Tech tales from our powerhouse Software Engineering team!

Responses (1)