question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

only render a component on client-side

See original GitHub issue

I’m trying to use react-select and got an error throw document is not defined. I assume this is because it’s trying to render the component on the server side and there’s no document.

What’s the best way to only render a specific component only on the client side?

<bountysource-plugin>

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource. </bountysource-plugin>

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
hellaiscommented, Dec 10, 2016

I tried the canUseDOM solution and it worked for me. However it is showing a warning, invalid checksum about the markup being generated is different on the client. I think this means React redraws the markup on the client-side.

I also ran into this issue. As a way to resolve it used a state variable to ensure the client-side only module is rendered after the first render, like this:

import React, { PropTypes } from 'react';

class MyComponent extends React.Component {

  constructor(props) {
    super(props);
    this.state = {appIsMounted: false};
  }

  componentDidMount() {
    requestAnimationFrame(() => {
      this.setState({ appIsMounted: true });
    });
  }

  render() {
    return (
      <div>
      { this.state.appIsMounted
        && React.createElement(
          require('../../client-side-only').default
        )
      }
      </div>
    );
  }
}

Hope this helps somebody else…

2reactions
markserbolcommented, Nov 3, 2015

I tried the canUseDOM solution and it worked for me. However it is showing a warning, invalid checksum about the markup being generated is different on the client. I think this means React redraws the markup on the client-side. Is it possible to get rid of this warning?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Render client-side only component in Next.js
If you are familiar with Next.js then you will know it is the React SSR (server-side rendering) framework created by Vercel.
Read more >
How to Client-Side-Render a Component in Next.js
The trick is so simple, actually: When we render something on the server, the window object isn't available to our code. Why? Well,...
Read more >
How do I make a client-side only component for GatsbyJS?
How do I create a component for Gatsby that will load on the client-side, not at build time? I created this one and...
Read more >
Rendering: Server and Client Components - Next.js beta docs
Client Components are rendered on the client. With Next.js, Client Components can also be pre-rendered on the server and hydrated on the client....
Read more >
Client Side Rendering Vs Server Side Rendering in React js ...
In server-side rendering when a user makes a request to a webpage, the server prepares an HTML page by fetching user-specific data and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found