Skip to content

Instantly share code, notes, and snippets.

@mchurichi
Created May 10, 2018 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mchurichi/00d020dd959533e009ce2f21d92a744f to your computer and use it in GitHub Desktop.
Save mchurichi/00d020dd959533e009ce2f21d92a744f to your computer and use it in GitHub Desktop.
React's Higher-Order Component to show a Semantic-UI-React Loading spinner
import React, { Component } from 'react';
import { Loader } from 'semantic-ui-react';
const withLoader = (WrappedComponent, dataProps) => (
class LoaderHOC extends Component {
render() {
const prop = this.props[dataProps];
return (!prop || Object.keys(prop).length === 0)
? <Loader active />
: <WrappedComponent {...this.props} />;
}
}
);
export default withLoader;
@mchurichi
Copy link
Author

Usage:

import React, { Component } from 'react';
import PropTypes from 'prop-types';

export class ContactCard extends Component {

  static propTypes = {
    person: PropTypes.object.isRequired;
  }

  render() {
    const { name, lastName } = this.props.person;

    return (
      <div>{lastName}</div>
      <div>{name}</div>
    )
  }

}

export default withLoader(ContactCard, 'person');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment