壁纸壁布施工航飞曹 發表於 2025-1-5 08:44:00

[cause]: TypeError: e_.createContext is not a function (Next.js 15)

<p>开发 Next.js 项目遇到报错:&nbsp;: TypeError: e_.createContext is not a function&nbsp;</p>
<p>出现这个报错的原因是在 Next.js 项目中,在&nbsp;<code>Server Component</code>&nbsp;中使用了MUI组件,但是MUI组件没有做 SSR 适配就会导致这个报错。</p>
<h2 id="解决办法">解决办法</h2>
<p>解决办法就是在文件顶部添加&nbsp;<code>use client</code>&nbsp;声明,让组件变成&nbsp;<code>Client Component</code></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">'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 = () =&gt; {
const = React.useState(false);
const = React.useState('');
const = React.useState</span><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">'success</span><span style="color: rgba(255, 0, 0, 1)">' | 'error'</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">('success');

const handleChange = async (event: React.ChangeEvent</span><span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">HTMLInputElement</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)">) =&gt; {
    const file = event.target.files?.;
    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 = () =&gt; {
    setOpen(false);
};

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

export default App;</span></pre>
</div>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <br /><p><table><tr><td><img style="border-top-width: 0pt; border-left-width: 0pt; border-bottom-width: 0pt; border-right-width: 0pt" alt="Creative Commons License" src="http://i.creativecommons.org/l/by-nc/2.5/cn/88x31.png" /> </td><td>本作品采用 知识共享署名-非商业性使用 2.5 中国大陆许可协议进行许可。 </td></p><br><br>
来源:https://www.cnblogs.com/sekihin/p/18652972
頁: [1]
查看完整版本: [cause]: TypeError: e_.createContext is not a function (Next.js 15)