What does registerServiceWorker do in React JS?

JavascriptReactjsService WorkerReact Dom

Javascript Problem Overview


I'm a newbie in React and I was wondering what is the purpose of registerServiceWorker() in the following code?

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

Javascript Solutions


Solution 1 - Javascript

The service worker is a web API that helps you cache your assets and other files so that when the user is offline or on slow network, he/she can still see results on the screen, as such, it helps you build a better user experience, that's what you should know about service worker's for now. It's all about adding offline capabilities to your site.

React creates a service worker for you without your configuration by default. To learn more;

Solution 2 - Javascript

The service worker is to use the website data offline in case there is no network, it's like making PWA. Refer to this link PWA to know more about implementation and how to use registerServiceWorker().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser7232873View Question on Stackoverflow
Solution 1 - Javascriptmostafa touradView Answer on Stackoverflow
Solution 2 - JavascriptYogesh DevgunView Answer on Stackoverflow