Skip to content

Instantly share code, notes, and snippets.

@EddiG
Last active October 20, 2017 05:13
Show Gist options
  • Save EddiG/3c55b8c1371d9cad214a72916dcc583b to your computer and use it in GitHub Desktop.
Save EddiG/3c55b8c1371d9cad214a72916dcc583b to your computer and use it in GitHub Desktop.

The example of how to render something only on client side

Inspired by facebook/react#10923 (comment)

class OnlyOnClient extends Component {
  static propTypes = {
    placeholder: PropTypes.node,
    html: PropTypes.string
  };

  state = {
    onClient: false
  };

  shouldComponentUpdate() {
    return !this.state.onClient;
  }

  componentDidMount() {
    this.setState({
      onClient: true
    });
  }

  render() {
    if (!this.state.onClient) {
      return this.props.placeholder;
    }

    return <div dangerouslySetInnerHTML={{ __html: this.props.html }} />;
  }
}

const Graphics = () => (
  <div>
    This text can re-render
    <OnlyOnClient
      placeholder={
        <div style={{ height: 200 }}>
          Try to reserve best guess height, placeholder graphic, etc
        </div>
      }
      html={"<div>d3 graphics etc</div>"} // this won't re-render
    />
    Okay to re-render here too
  </div>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment