Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ react-dropzone with web worker


๐Ÿ“š react-dropzone with web worker


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Motivation

Once I had a weird experience with large images and some stuff like images and react-dropzone. It was too laggy with large ones and it blocked some UI elements.

Prerequisites

  • react and stuff chained with it
  • react-dropzone

Problematic

I have written an example with the default preset for react-dropzone and just trying to upload some large img.

First code block (such as default preset):

import { useCallback, useState } from 'react'
import { useDropzone } from 'react-dropzone'

export const Dropzone = () => {
    const [state, setState] = useState<string | undefined>(undefined)
    const onDrop = useCallback((acceptedFiles: File[]) => {
        acceptedFiles.forEach((file) => {
            const reader = new FileReader()

            reader.onload = () => {
                const result = reader.result as any

                setState(result)
            }

            reader.readAsDataURL(file)
        })
    }, [])

    const { getRootProps, getInputProps } = useDropzone({ onDrop })

    return (
        <div {...getRootProps()}>
            <input {...getInputProps()} />
            <p>Drag 'n' drop some files here, or click to select files</p>
            {state && <img src={state} style={{ width: 200, height: 200 }} />}
        </div>
    )
}

We can try to use input while we trying to upload an image and ... it's gone.

So I tried many things and maybe u can provide ur own solutions to comments or will create another article about it, will appreciate it.

Solution

The solution is to use Web Worker to unload our operation.
The preload by new Image() does not work.
Web Worker has no Image at all, though.

worker.js

onmessage = async function (event) {
    const response = await fetch(event.data.result)
    const blob = await response.blob()
    postMessage(URL.createObjectURL(blob))
}

index.tsx

import { useCallback, useEffect, useState } from 'react'
import { useDropzone } from 'react-dropzone'

export const Dropzone = () => {
    const [state, setState] = useState<string | undefined>(undefined)
    const [worker, setWorker] = useState<Worker | null>(null)
    const onDrop = useCallback(
        (acceptedFiles: File[]) => {
            acceptedFiles.forEach((file) => {
                const reader = new FileReader()

                reader.onload = () => {
                    const result = reader.result as string
                    if (worker) {
                        worker.postMessage({ result })
                    }
                }

                reader.readAsDataURL(file)
            })
        },
        [worker]
    )

    const { getRootProps, getInputProps } = useDropzone({ onDrop })

    useEffect(() => {
        const workerInstance = new Worker(new URL('worker.js', import.meta.url))
        workerInstance.onmessage = function ({ data }) {
            setState(data)
        }

        setWorker(workerInstance)

        return () => workerInstance.terminate()
    }, [])
    return (
        <div {...getRootProps()}>
            <input {...getInputProps()} />
            <p>Drag 'n' drop some files here, or click to select files</p>
            {state && <img src={state} style={{ width: 200, height: 200 }} />}
        </div>
    )
}

...



๐Ÿ“Œ Web Extract Worker, a service with text-to-image worker AI models!


๐Ÿ“ˆ 31.41 Punkte

๐Ÿ“Œ This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


๐Ÿ“ˆ 30.43 Punkte

๐Ÿ“Œ This Week In React #185: React Conf, React Query, refs, Next.js after, mini-react...


๐Ÿ“ˆ 30.43 Punkte

๐Ÿ“Œ Tesla Factory Worker Living in His Car Awoken by Molotov Cocktails Thrown by Ex Factory Worker


๐Ÿ“ˆ 27.84 Punkte

๐Ÿ“Œ After Amazon Increases Worker Wages, Whole Foods Responds By Cutting Worker Hours


๐Ÿ“ˆ 27.84 Punkte

๐Ÿ“Œ Handling React OTP Input Auth Web | React Native using react-otp-kit package


๐Ÿ“ˆ 26.39 Punkte

๐Ÿ“Œ Handling React OTP Input Auth Web | React Native using react-otp-kit package


๐Ÿ“ˆ 26.39 Punkte

๐Ÿ“Œ This Week In React #127: Nextra, React-Query, React Documentary, Storybook, Remix, Tamagui, Solito, TC39, Rome...


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ This Week In React #131: useReducer, Controlled Inputs, Async React, DevTools, React-Query, Storybook, Remix, RN , Expo...


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ This Week In React #139: React.dev, Remix, Server Components, Error Boundary, Wakuwork, React-Native, Bottom Sheet...


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ This Week In React #142: React-Query, Million, OpenNext, Ariakit, Expo-Image, React-Three-Fiber, TS 5.1, Node.js 20, WebGPU...


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ This Week In React #146: Concurrency, Server Components, Next.js, React-Query, Remix, Expo Router, Skia, React-Native...


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ This Week In React #188 : React 19 RC0, Data Fetching, Framer Motion, Compiler, Astro, Zod, Remix, Docusaurus, React-Query...


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ Whatโ€™s New in React 19? React Canaries, Actions, and React Compiler


๐Ÿ“ˆ 22.83 Punkte

๐Ÿ“Œ Share code between React Native and React Web


๐Ÿ“ˆ 18.78 Punkte

๐Ÿ“Œ Animated countdown in react with @react-spring/web


๐Ÿ“ˆ 18.78 Punkte

๐Ÿ“Œ Working with live data in the service worker - Progressive Web App Training


๐Ÿ“ˆ 17.49 Punkte

๐Ÿ“Œ Mozilla Firefox up to 57.x Web Worker Use-After-Free denial of service


๐Ÿ“ˆ 17.49 Punkte

๐Ÿ“Œ heise+ | JavaScript beschleunigen: So parallelisieren Web Worker Prozesse


๐Ÿ“ˆ 17.49 Punkte

๐Ÿ“Œ Mozilla Firefox 26 Web Worker Error Message credentials management


๐Ÿ“ˆ 17.49 Punkte

๐Ÿ“Œ Mozilla Firefox 26 Web Worker asm.js resource management


๐Ÿ“ˆ 17.49 Punkte

๐Ÿ“Œ CVE-2015-7197 | Mozilla Firefox 41 Web Worker access control (MFSA 2015-13 / BID-77411)


๐Ÿ“ˆ 17.49 Punkte

๐Ÿ“Œ CVE-2022-22760 | Mozilla Thunderbird up to 91.5 Web Worker information exposure (Bug 1740985)


๐Ÿ“ˆ 17.49 Punkte

๐Ÿ“Œ Using Web Worker API In Your Reactjs Project To Enhance Performance


๐Ÿ“ˆ 17.49 Punkte

๐Ÿ“Œ Invoking Web Worker as a module


๐Ÿ“ˆ 17.49 Punkte

๐Ÿ“Œ For web worker! 5 extensions that will help you your work done


๐Ÿ“ˆ 17.49 Punkte

๐Ÿ“Œ React Fiber: Facebook baut Javascript-Bibliothek React fundamental um


๐Ÿ“ˆ 15.22 Punkte

๐Ÿ“Œ The State of React Native Tooling (React Native CLI - The Ultimate Guide)


๐Ÿ“ˆ 15.22 Punkte

๐Ÿ“Œ heise+ | Externe Bibliotheken fรผr React: Material-UI und React Router einbinden


๐Ÿ“ˆ 15.22 Punkte

๐Ÿ“Œ How to Test Your React App Effectively with React Testing Library


๐Ÿ“ˆ 15.22 Punkte

๐Ÿ“Œ Fix the "React Hook is Called Conditionally" Error in React


๐Ÿ“ˆ 15.22 Punkte

๐Ÿ“Œ Build a 3D World in React with ThreeJS and React Three Fiber


๐Ÿ“ˆ 15.22 Punkte

๐Ÿ“Œ Typescript for React Components (or How To Write Components in React The Right Way)


๐Ÿ“ˆ 15.22 Punkte

๐Ÿ“Œ React Redux Tutorial #1 - Was ist React Redux


๐Ÿ“ˆ 15.22 Punkte

๐Ÿ“Œ React Redux Tutorial #1 - Was ist React Redux


๐Ÿ“ˆ 15.22 Punkte











matomo