Skip to content

Instantly share code, notes, and snippets.

@sirgallifrey
Created August 5, 2017 02:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sirgallifrey/04a35eec027a081729b833cbef002d81 to your computer and use it in GitHub Desktop.
Save sirgallifrey/04a35eec027a081729b833cbef002d81 to your computer and use it in GitHub Desktop.
is loged In HOC
import React from 'react';
import Loader from '../components/loader';
export default function fetchData(url, options) {
return (Component) => class fetchComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
finished = false;
data = null;
}
}
componentDidMount() {
fetch(url, options)
.then(r => r.json())
.then(data => this.setState({ finished: true, data }))
}
render() {
return (this.state.finished ? <Component {...props} data={this.state.data}> : <Loading/>);
}
}
}
import React from 'react';
import Auth from '../lib/auth';
export function ifLoggedIn(Component) {
return (props) => (
Auth.isLoggedIn ? <Component {...props}/> : null
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment