react-dropzone: document is not defined

Do you want to request a feature or report a bug?

  • [ x] I found a bug
  • I want to propose a feature

What is the current behavior? Version 10.0.0 cause a document undefined error with server side rendering.

If the current behavior is a bug, please provide the steps to reproduce.

  1. Enable application with SSR
  2. Use react-dropzone

What is the expected behavior? no undefined error

Please mention other relevant information such as the browser version, Operating System and react-dropzone version.

Most likely caused from changing /src/utils/index.js

from

export const supportMultiple =
  typeof document !== 'undefined' && document && document.createElement
    ? 'multiple' in document.createElement('input')
    : true

to

export const supportMultiple = 'multiple' in document.createElement('input')

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 17
  • Comments: 19

Commits related to this issue

Most upvoted comments

@Chris533 , it helps me, thx. @Dakkers try this:

import dynamic from 'next/dynamic';
const Dropzone = dynamic(() => import('react-dropzone'), { ssr: false });

const App = () => {
    return (
       <Dropzone>
            {({ getRootProps, getInputProps }) => (
                <div {...getRootProps()} className={classes.dropzone}>
                  <input {...getInputProps()} />
                  <div className={classes.itemOutContainer}>
                    <div className={classes.itemContainer}>
                      <div className={cns(classes.item, classes.itemCreate)}>
                        uploading files
                      </div>
                    </div>
                  </div>
                </div>
            )}
       </Dropzone>
    );
}

๐ŸŽ‰ This issue has been resolved in version 10.0.4 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€

Just change export const supportMultiple = 'multiple' in document.createElement('input')

to

export const supportMultiple = 'multiple' in React.createElement('input')

and the problem should be solved. In case you donโ€™t want to import React in utils/index.js just move supportMultiple to src/index.js since it is just used there

import dynamic from 'next/dynamic'

const DynamicComponentWithNoSSR = dynamic(() => import('../components/hello3'), {
  ssr: false
})

function Home() {
  return (
    <div>
      <Header />
      <DynamicComponentWithNoSSR />
      <p>HOME PAGE is here!</p>
    </div>
  )
}

export default Home

Thanks, I user this finger out.

useEffects are run after a component is mounted, which is safe for SSR however, the code above gets parsed by the browser on the page load and thatโ€™s the cause of the error. Adding a guard clause that checks for DOM would solve the problem.

This is the cause of the issue:

// Change
export const supportMultiple = 'multiple' in document.createElement('input')

// TO

export const supportMultiple =
  typeof document !== 'undefined' && document && document.createElement
    ? 'multiple' in document.createElement('input')
    : true

Happy to make a PR