reactjs - TypeError: (0, _react.default) 不是函数
问题描述
我正在为我的应用程序使用codesandbox.io,它返回错误TypeError : (0, _react.default ) is not a function。App.js
在我尝试从反应中导入useState之前,这个主代码运行良好,就像这样。
// The error reference appears on the line that is defined by the isOpen variable and the openSidebar function
import useState, {React} from "react";
export default function App () {
const [isOpen, openSidebar] = useState (false);
return (
isOpen和openSidebar用于
<Sidebar
sidebar = {
<>
<h1> Sidebar content </h1>
<h1> Sidebar content </h1>
<h1> Sidebar content </h1>
<h1> Sidebar content </h1>
</>
}
open = {isOpen}
styles = {{sidebar: {background: "white", height: "100%"}}}
>
<Button onClick = {openSidebar (! IsOpen)}> Open sidebar </Button>
</Sidebar>
文件:
import "./styles.css";
import {Navbar, Nav, Container, Button} from "react-bootstrap";
import Sidebar from "react-sidebar";
import React, {useState} from "react";
export default function App () {
const [isOpen, openSidebar] = useState (false);
return (
<div className = "App">
<Navbar bg = "dark" style = {{color: "white", zIndex: 100}}>
<Container>
<Navbar.Brand href = "# home" style = {{color: "gray"}}>
home
</Navbar.Brand>
<Nav className = "me-auto">
<Nav.Link href = "# home" style = {{color: "gray"}}>
Info
</Nav.Link>
</Nav>
</Container>
</Navbar>
<div>
<Sidebar
sidebar = {
<>
<h1> Sidebar content </h1>
<h1> Sidebar content </h1>
<h1> Sidebar content </h1>
<h1> Sidebar content </h1>
</>
}
open = {isOpen}
styles = {{sidebar: {background: "white", height: "100%"}}}
>
<Button onClick = {() => openSidebar(!IsOpen)}> Open sidebar </Button>
</Sidebar>
</div>
</div>
);
}
我认为重新启动沙箱可能会修复一些错误。
解决方案
React
是默认导出并且useState
是命名导出。因此,将您的导入行更改为
import React, { useState } from 'react';
推荐阅读
- c++ - 为什么在 std::vector::push_back(T object) 方法中构造对象时调用析构函数?
- python - 如何在 Python 中通过 csvReader 使用高阶函数
- python - Visual Studio 代码:终端进程以退出代码终止:{0}
- javascript - 在 UCI 中使用 JavaScript 从 HTML 中过滤 Dynamics CRM 帐户主页
- c# - Unity UI 文本在游戏视图中正确显示,但在构建中不显示
- javascript - 日期更改间隔
- matlab - 1不等于1?MATLAB中的矩阵条目比较
- .net - 从 .net 核心 API 返回纯文本/非 JSON 数据
- r - 具有两种字体大小的单行 ggplot2 标题
- python - Numpy 函数输出随着每次运行而增加