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.

Allow rootRef/inputRef to be passed into useDropzone

See original GitHub issue
  • I want to propose a feature

What is the current behavior?

The docs state:

Important: do not set the ref prop on the elements where getRootProps()/getInputProps() props are set, instead, get the refs from the hook itself

In the code, rootRef and inputRef are defined and defaulted inside the hook: https://github.com/react-dropzone/react-dropzone/blob/951ecb2a03b16e192a2b0b4c8ca3d7d06061a9f1/src/index.js#L391-L392

This is fine within the scope of the component directly using dropzone, but if an external (eg. parent) component wants to pass in a ref… suddenly we need to resort to ‘syncing logic’, rather than being able to just use the same ref, eg.:

const dropzone = useDropzone()
const { ref: containerRef } = dropzone.getRootProps()

useEffect(
    () => {
      if (containerRef.current !== forwardedRef.current) {
        forwardedRef.current = containerRef.current
      }
    },
    [forwardedRef, containerRef]
  )

What is the expected behavior?

It would be nice to be able to pass in our own refs, and have dropzone use them as it’s internal refs.

eg.

const dropzone = useDropzone({
  rootRef: myRootRef,
  inputRef: myInputRef,
})

Obviously, if we don’t pass any in, then it should default them.

If this is a feature request, what is motivation or use case for changing the behavior?

Mostly described above, but basically the workflow is a bit janky feeling currently if we want to sync with externally provided refs.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:5

github_iconTop GitHub Comments

5reactions
vlveretacommented, May 6, 2020

Hi there! Is anyone working on this feature? Or, maybe it’s already implemented somehow? I’m totally tired trying to find a perfect shape solution for passing ref from outside (parent component) into Dropzone (child component) using hooks (useDropzone) 😓 I think it would be great to have such a possibility as above ☝️

3reactions
smelllllllllllcommented, May 14, 2020

From an accessibility point of view it would be useful to be able to access the rootRef so you can dyamicaly return focus to it after closing a modal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-dropzone
Both getRootProps and getInputProps accept a custom refKey (defaults to ref ) as one of the attributes passed down in the parameter.
Read more >
Accepting specific file types - react-dropzone
By providing accept prop you can make the dropzone accept specific file types and reject the others. The value must be an object...
Read more >
How to use dropzone in a single file mode? - Stack Overflow
You can pass on multiple: false to useDropzone and it will ignore multiple files on drop and only the first one will be...
Read more >
React Dropzone and File Uploads in React - Upmostly
React Dropzone only needs one method passed into the onDrop prop to handle ... Currently, our file picker allows us to pick any...
Read more >
react-dropzone - npm
Both getRootProps and getInputProps accept a custom refKey (defaults to ref ) as one of the attributes passed down in the parameter.
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