Skip to content

Instantly share code, notes, and snippets.

@distributedlife
Created February 22, 2017 00:53
Show Gist options
  • Save distributedlife/99f27950a7b9d205a9819a12033898c3 to your computer and use it in GitHub Desktop.
Save distributedlife/99f27950a7b9d205a9819a12033898c3 to your computer and use it in GitHub Desktop.
React component that only provides lifecycle events
import React from 'react';
import { connect } from 'react-redux';
import { browserHistory } from 'react-router';
import makeLifecyleComponent from './lifecycle-only';
const redirectToRoot = (props) => {
if (!props.isLoggedIn) {
browserHistory.push('/');
}
};
export const RedirectToLoginWhereNoToken = connect((state) => ({
isLoggedIn: !!state.auth.token,
}))(makeLifecyleComponent('RedirectToLoginWhereNoToken', { componentDidMount: redirectToRoot }));
import React from 'react';
export default (name, lifecycleEvents) => React.createClass({
displayName: name,
render() {
return null;
},
componentDidMount() {
return lifecycleEvents.componentDidMount(this.props);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment