spinner for each component in react

react-spinners example
react loading component example
react-bootstrap spinner
react-spinners examples
react-loader-spinner github
react loading spinner-overlay
axios loading spinner react
react loader spinner codepen

What is the best practice and efficient way for implement spinner component for whole apps ?

My sample code :

render() {
  if (this.state.loading)
    return (
      <Spinner/>
    )

  return (
    <div className="container">
  )
}

i want avoid to rewrite the same logic checking loading state for each component. TIA.

make a utils folder and make a file (sppiner.js) in that file and write this code

spinner.js

export default function spinner({text}) {
  return (
    <div>
      <Spinner/>
      <span>{text}</span>
    </div>
  )

and after that in each component that you want use spinner write this code

import Spinner from "utils/spinner";

return(
...
{this.state.loading && <Spinner text={"your text..."} />}
...
)

react-spinners, A collection of react loading spinners. Each loader accepts a loading prop as a boolean. The loader will render null if loading Component {. Lets create a react loading spinner! For this react loading spinner tutorial, I’ll use a custom function that resolves after given seconds. I’ll also use the GitHub API to fetch my details. In both cases, the loading component will be visible until the data is fully loaded. Setting Up

Simply apply a ternary:

render() {
  return this.state.loading ? <Spinner/> : <App />
}

And make it a function:

loader.js

import Spinner from './spinner.js';
export default function LoadManager = ({loading}) => props.loading ? <Spinner /> : props.children;

So in your components

render() {
  return 
    <LoadManager loading={this.state.loading}>
      <App />
    </LoadManager>
}

Other more common ways involves use of React Loadable:

import Loadable from 'react-loadable';
import Loading from './my-loading-component';

const LoadableComponent = Loadable({
  loader: () => import('./my-component'),
  loading: Loading,
});

export default class App extends React.Component {
  render() {
    return <LoadableComponent/>;
  }

This is more efficient too, has the code will be splitted.

Spinners, When the details are fetched, showDetail is rendered with all the details. App Component. App initially renders Loader component, this will� Usage. Each loader has their own default properties. You can overwrite the defaults by passing props into the loaders. Each loader accepts a loading prop as a boolean. The loader will render null if loading is false.

What about adding this kind of function?

function renderSpinner() {
  return this.state.loading && <Spinner />
}

Create React loading spinner, Updated to react-promise-tracker v 2.0 If true, then display the loading spinner. to a microlibrary like React Promise Tracker, which will take care of all those edge import React, { Component } from 'react'; import { render } from 'react-dom ';� Usage. Each loader has their own default properties. You can overwrite the defaults by passing props into the loaders. Each loader accepts a loading prop as a boolean. The loader will render null if loading is false.

React how to display a loading indicator on fetch calls, If all you want is access to the @chevtek/react-spinners package then What if you want to show/hide a spinner in a separate component tree� React Bootstrap Loader/Spinner React Loader/Spinner - Bootstrap 4 & Material Design. React Bootstrap loader is animation that is used to keep visitors entertained while the page is still loading, which helps to increase the user experience.

How to do loading spinners, the React way., react-spinner-loader provides simple React SVG spinner component which can be implemented for async await operation before data loads to the view. A simple React component made using canvas and window frame animations. Very close in resemblance to that of the Spinner in Android or the loading spinner on YouTube.

mhnpd/react-loader-spinner: Collection set of react-spinner , react-spinners documentation, tutorials, reviews, alternatives, A collection of loading spinner components for react 236 � See all & compare� Suspense for Data Fetching is a new feature that lets you also use <Suspense> to declaratively “wait” for anything else, including data. This page focuses on the data fetching use case, but it can also wait for images, scripts, or other asynchronous work.

Comments
  • Trying to avoid duplicating a single if seems like over-optimisation to me, and you'll probably just end up making your project more complicated by trying to avoid it.
  • This is an opinionated question. Ternary or if...else both are conditional statements and will be very similar in performance. Also, this looks like micro optimization. Instead look at the logic and optimize that
  • ok your wellcome if you like this answer please accept it thank you @cumibulat
  • How is this more efficient?
  • This is simply cleaner, with a single return and no if, cause the condition has only 2 possible output based on a boolean
  • Correct! This is the same code written in different style. So basically its not resolving anything. If OP is not satisfied with performance of if...else, this is going to be same. You may say about bundle size as it will take less lines but the if you use bundler like webpack/ gulp, that's done automatically. So the code should be more readable instead
  • @Rajesh the OP simple wants to do not repeat the code ore make it less heavy to bring in other components. There is no code splitting issues or performances involved.
  • The whole App component with all children will be re-renderer if spinner toggles. It will impact performance a lot., @mohammad answer is more efficient.
  • Notice that with && operator, the right part of the operator is evaluated only if the first is truthy, otherwise the left part will be returned. In this case it would render false if loading is false, if loading is 0 it will render "0" as string.