How to use rxjs module in ReactJS ?
Last Updated :
25 Jul, 2024
RXJS stands for Reactive Extensions for JavaScript. This module provides the implementation of observable type to work with reactive programming which is an asynchronous programming paradigm. We can use the following approach in ReactJS to use the rxjs module.
Approach: We have used the Rxjs module to demonstrate how we can use it in our ReactJS application. We can create a user-defined printData function that has a range function starting from 1 to 50, and it filters the number whose modulo 2 is equal to 1 and maps it to multiply itself, and then we print the resulting number in our console screen. We have a button on the UI and if the user clicks, this printData function is called.
Prerequisites:
Steps to Create the React Application And Installing Module:
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. folder name, move to it using the following command:
cd foldername
Step 3: After creating the ReactJS application, Install the required module using the following command:
npm install rxjs
Project Structure:
Project StructureThe updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"rxjs": "^7.8.1",
"web-vitals": "^2.1.4",
}
Example: Now write down the following code in the App.js file.
App.js
import React from 'react';
import { range } from "rxjs";
import { map, filter } from "rxjs/operators";
const printData = () => {
range(1, 50).pipe(filter(x => x % 2 === 1),
map(x => x * x))
.subscribe((item) => {
console.log("Item: ", item)
});
}
export default function App() {
return (
<div style={{ display: 'block',
width: 700, padding: 30 }}>
<h4>RXJS Module</h4>
<div style={{ width: 660, height: 'auto' }}>
<button onClick={printData}>
Call Function
</button>
</div>
</div>
);
}
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000
Similar Reads
How to modularize code in ReactJS ? Modularize code in React JS refers to dividing it into segments or modules, where each file is responsible for a feature or specific functionality. React code can easily be modularized by using the component structure. The approach is to define each component into different files. With each componen
3 min read
How to use events in ReactJS ? Modern webpages rely on user interactions, triggering events like clicks or keypresses. React facilitates event handling with built-in methods, allowing developers to create listeners for dynamic interfaces and responses. JavaScript events, inherent to the DOM, use bubbling propagation by default, m
2 min read
How to export modules with sub-modules in ReactJS ? One of the key features of React is its modular structure, which allows developers to break down their code into reusable components. In many cases, these components are organized into modules and submodules, which can be exported and imported into other parts of the application. Prerequisites:NPM
3 min read
ReactJS | Hot Module Replacement It is always recommended to start your react journey using the create-react-app which can be found here. It saves a lot of time as all the basic application backend is provided after installation, and we are only left to deal with the implementation details. Whenever we make changes in the main core
3 min read
How to use ReactJS with HTML Template ? We all know ReactJS is a front-end development framework. For the front end, we need to create HTML templates. So, this article teaches you how to use HTML templates in ReactJS. The process is very simple. Prerequisites:NodeJS or NPMReact JSApproach: We will create a simple one-page HTML template i
4 min read
How to dynamically load module in React.js ? In React JS loading of modules statically is cumbersome as it loads the modules beforehand even if it is not rendered. Some components render only when the user interacts, so in such cases, it can lead to more resource consumption. When you statically import modules, you are loading larger data than
3 min read