惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
J
Java Code Geeks
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
雷峰网
雷峰网
D
DataBreaches.Net
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 聂微东
V
Visual Studio Blog
Apple Machine Learning Research
Apple Machine Learning Research
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Martin Fowler
Martin Fowler
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
Engineering at Meta
Engineering at Meta

博客园 - sekihin

【SQLSERVER】备份还原除当前数据库~之外的其他数据库的bak备份 【前端】常用VsCode插件 React席哪个能优化 20 GitHub 仓库帮助你成为 React专家 Export a named export for each HTTP method instead.(Next.js 15) Error occurred prerendering page "/_not-found".(Next.js 15) Error: Attempted to call generateViewport() from the server (Next.js 15) Cursor - AI代码编辑器的使用指南 Next.js项目中.prettierrc.json的配置 Next.js项目中.eslintrc.js的配置 nvm: Node Version Manager PHP slim 部署Apache NestJS 部署Apache NestJS导出API文档 ChatGPT plugins Obisidian plugins Build nest.js by tsconfig.json Data Transfer Objects (DTOs) in NestJS TypeError: stringWidth is not a function
[cause]: TypeError: e_.createContext is not a function (N...
sekihin · 2025-01-05 · via 博客园 - sekihin

开发 Next.js 项目遇到报错: [cause]: TypeError: e_.createContext is not a function 

出现这个报错的原因是在 Next.js 项目中,在 Server Component 中使用了MUI组件,但是MUI组件没有做 SSR 适配就会导致这个报错。

解决办法

解决办法就是在文件顶部添加 use client 声明,让组件变成 Client Component

'use client';  // 加上这行

import React from 'react';
import UploadIcon from '@mui/icons-material/Upload';
import Button from '@mui/material/Button';
import Snackbar from '@mui/material/Snackbar';
import Alert from '@mui/material/Alert';
import { styled } from '@mui/material/styles';
import axios from 'axios';

const Input = styled('input')({
  display: 'none',
});

const App: React.FC = () => {
  const [open, setOpen] = React.useState(false);
  const [message, setMessage] = React.useState('');
  const [severity, setSeverity] = React.useState<'success' | 'error'>('success');

  const handleChange = async (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0];
    if (file) {
      try {
        const formData = new FormData();
        formData.append('file', file);

        const response = await axios.post('https://660d2bd96ddfa2943b33731c.mockapi.io/api/upload', formData, {
          headers: {
            authorization: 'authorization-text',
            'Content-Type': 'multipart/form-data',
          },
        });

        if (response.status === 200) {
          setMessage(`${file.name} file uploaded successfully`);
          setSeverity('success');
        }
      } catch (error) {
        setMessage(`${file.name} file upload failed.`);
        setSeverity('error');
      } finally {
        setOpen(true);
      }
    }
  };

  const handleClose = () => {
    setOpen(false);
  };

  return (
    <>
      <label htmlFor="upload-file">
        <Input accept="image/*" id="upload-file" type="file" onChange={handleChange} />
        <Button variant="contained" component="span" startIcon={<UploadIcon />}>
          Click to Upload
        </Button>
      </label>
      <Snackbar open={open} autoHideDuration={6000} onClose={handleClose}>
        <Alert onClose={handleClose} severity={severity} sx={{ width: '100%' }}>
          {message}
        </Alert>
      </Snackbar>
    </>
  );
};

export default App;