最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReferenceError: FileReader is not defined in Next.js - Stack Overflow

programmeradmin1浏览0评论

I am trying out the new functions of Next.js 13 with the /app folder, but in a simple client-side ponent that handles an input form, I am trying to use FileReader but receive an error when browsing.

This is the summary of the code:

"use client";
import React, { useState, useEffect } from "react";
import { useRouter } from "next/navigation";
import useStore from "../app/store";

export default function FileUploader() {
  const [files, setFiles] = useState([]);
  const router = useRouter();
  const addFile = useStore((state) => state.addFile);

  const fileReader = new FileReader();

  const onFileSelect = (event) => {
    event.preventDefault();
    setFiles(event.target.files);
    console.log(event.target.files);
  };

  useEffect(() => {
    if (files[0] == null) return;
    let FileToString = fileReader.readAsText(files[0]); // get error at this line
    addFile(FileToString);

    router.push("/file/" + filename);
  }, [files]);

  return (
    <input
      id="dropzone-file"
      type="file"
      className="fileInput"
      onChange={onFileSelect}
    />
  );
}

Error:

wait  - piling...
event - piled client and server successfully in 3.4s (758 modules)
ReferenceError: FileReader is not defined
    at FileUploader (webpack-internal:///(sc_client)/./ponents/FileUploader.js:27:24)
    ...

What am I doing wrong?

I am trying out the new functions of Next.js 13 with the /app folder, but in a simple client-side ponent that handles an input form, I am trying to use FileReader but receive an error when browsing.

This is the summary of the code:

"use client";
import React, { useState, useEffect } from "react";
import { useRouter } from "next/navigation";
import useStore from "../app/store";

export default function FileUploader() {
  const [files, setFiles] = useState([]);
  const router = useRouter();
  const addFile = useStore((state) => state.addFile);

  const fileReader = new FileReader();

  const onFileSelect = (event) => {
    event.preventDefault();
    setFiles(event.target.files);
    console.log(event.target.files);
  };

  useEffect(() => {
    if (files[0] == null) return;
    let FileToString = fileReader.readAsText(files[0]); // get error at this line
    addFile(FileToString);

    router.push("/file/" + filename);
  }, [files]);

  return (
    <input
      id="dropzone-file"
      type="file"
      className="fileInput"
      onChange={onFileSelect}
    />
  );
}

Error:

wait  - piling...
event - piled client and server successfully in 3.4s (758 modules)
ReferenceError: FileReader is not defined
    at FileUploader (webpack-internal:///(sc_client)/./ponents/FileUploader.js:27:24)
    ...

What am I doing wrong?

Share Improve this question edited Dec 31, 2022 at 19:46 Camilo 7,2245 gold badges45 silver badges66 bronze badges asked Dec 30, 2022 at 14:25 CerixCerix 831 silver badge6 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 8

Like accessing window, any browser-specific code in Next.js needs to run on the client. If you want to use FileReader, which is part of the browser API, add it to your useEffect, like so:

useEffect(() => {
  const fileReader = new FileReader();
  // ...
}, [files]);

This way, Next.js won't look for the definition of a FileReader while rendering your ponent on the server. Yes, even client ponents render first on the server, and get hydrated on the browser.

发布评论

评论列表(0)

  1. 暂无评论