Skip to content

Instantly share code, notes, and snippets.

@oliverbenns
Created November 20, 2017 08:31
Show Gist options
  • Select an option

  • Save oliverbenns/dcc7f2c5e23a7a63b5a39b40b9a1a75e to your computer and use it in GitHub Desktop.

Select an option

Save oliverbenns/dcc7f2c5e23a7a63b5a39b40b9a1a75e to your computer and use it in GitHub Desktop.

Revisions

  1. oliverbenns renamed this gist Nov 20, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. oliverbenns created this gist Nov 20, 2017.
    33 changes: 33 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    import React from 'react';

    const visible = isVisible => Component => {
    class VisibleComponent extends React.Component {
    componentDidMount() {
    this.unsubscribe = this.context.store.subscribe(this.handleChange.bind(this));
    }

    componentWillUnmount() {
    this.unsubscribe();
    }

    handleChange() {
    this.forceUpdate();
    }

    render() {
    const show = isVisible(this.context.store.getState());

    if (!show) {
    return null;
    }

    return <Component {...this.props} />;
    }
    }

    VisibleComponent.contextTypes = { store: React.PropTypes.object };

    return VisibleComponent;
    };

    export default visible;