- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为 Material UI 选项卡实现一个 React 类。我本质上是从 material ui 网站上取了标签的例子,并转换成类兼容格式。他们网站上的示例如下:
import React from 'react';
import PropTypes from 'prop-types';
import { makeStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Tabs from '@material-ui/core/Tabs';
import Tab from '@material-ui/core/Tab';
import Typography from '@material-ui/core/Typography';
function TabContainer(props) {
return (
<Typography component="div" style={{ padding: 8 * 3 }}>
{props.children}
</Typography>
);
}
TabContainer.propTypes = {
children: PropTypes.node.isRequired,
};
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
backgroundColor: theme.palette.background.paper,
},
}));
export default function SimpleTabs() {
const classes = useStyles();
const [value, setValue] = React.useState(0);
function handleChange(event, newValue) {
setValue(newValue);
}
return (
<div className={classes.root}>
<AppBar position="static">
<Tabs value={value} onChange={handleChange}>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
</AppBar>
{value === 0 && <TabContainer>Item One</TabContainer>}
{value === 1 && <TabContainer>Item Two</TabContainer>}
{value === 2 && <TabContainer>Item Three</TabContainer>}
</div>
);
}
这是我的 React 类风格的翻译代码。
import React from 'react'
import PropTypes from 'prop-types';
import { makeStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Tabs from '@material-ui/core/Tabs';
import Tab from '@material-ui/core/Tab';
import Typography from '@material-ui/core/Typography';
import MaterialTableDemo from './Table';
import Chart from './Chart';
// Define the styling of the tab component. The background of the
// displayed content is defined to be the paper color.
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
backgroundColor: theme.palette.background.paper,
},
}));
function TabContainer(props) {
return (
<Typography component="div" style={ { padding: 8 * 3} }>
{ props.children }
</Typography>
)
};
// TabContainer.propTypes = {
// children: PropTypes.node.isRequired,
// };
class Main extends React.Component {
constructor(props) {
// Inherit whatever props are given to it in the tag definition
super(props);
// Declaration of the state variable if needed
this.state = {
displayState: 0, // define the beginning state of the
// tab component
};
// Declaration of some member functions, some of whic
// return render-able HTML format code.
this.handleChange = this.handleChange.bind(this);
}
handleChange(newValue) {
this.setState( { displayState: newValue } )
}
// propTypes method only for debugging purposes.
// Will check for any inconsistencies. If a prop is required to be
// a node, but is not a node, will raise a warning/error.
render() {
const classes = useStyles();
return (
<div className={classes.root}>
<AppBar position="static" >
<Tabs value={this.state.displayState} onChange={this.handleChange}>
<Tab label="Chart" />
<Tab label="Table" />
</Tabs>
</AppBar>
{this.state.displayState === 0 && <TabContainer> <Chart/> </TabContainer>}
{this.state.displayState === 1 && <TabContainer> <MaterialTableDemo/> </TabContainer>}
</div>
);
}
}
export default Main;
我希望它运行并显示一个包含所需内容的选项卡。现在只是显示文本的虚拟组件。
当前错误是
Uncaught Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See [website deleted] for tips about how to debug and fix this problem.
最佳答案
错误发生是因为 useStyles()
有 makeStyles
是一个 react-hook
而你不能使用 react -hooks
在类组件中。正如您在示例中看到的那样,它使用的是功能组件。
render() {
const classes = useStyles(); // here is a react-hook that can't be used in class components
return (
关于javascript - React类实现中Uncaught Invariant Violation : Invalid hook call.问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56673528/
目前我能想到三种情况: 不包含任何资源的程序集 在其资源文件中仅包含图像(即 ToolBoxBitmaps)的控制库 仅包含用于抛出异常的文本的库(无论如何我们都不希望用户看到这些,对吗?;-)) 我
我正在学习一门强调使用不变量的中级编程类(class)。我以前从未使用过它们,它们似乎需要更多的时间来创建。软件工程行业是否强调使用不变量? 最佳答案 取决于你问谁 - 我使用不变量只是因为它让生活更
作为我学习的一部分,我认为我发现的抽象定义的最佳答案(有意义)来自 stackoverflow: What is abstraction? 除此之外, 作为 2006 年秋季伯克利在线类(class)
我正在试验 Code Contract,但遇到了一个问题。我有课: public class SpecialPoint { public int X { get; set; } pub
有人可以给我举个例子吗 invmap :: (a -> b) -> (b -> a) -> f a -> f b Invariant 有什么好处? 最佳答案 大多数情况下,人们不使用Invarian
我发现我的机器和构建服务器上的测试结果不同。我设法找到了不同的单行。这是一个字符串比较。这两个字符串的第一个字符不同。 下面的测试在我的本地机器上通过了,但在构建机器上失败了。 [TestClass]
尽管 _foo 不可能为 null,但下面的简单代码将由代码契约的静态检查器产生“不变的未经证实”的警告。该警告针对 UncalledMethod 中的 return 语句。 public class
我有一个像这样设置的 React 项目: 这是一个简单的应用程序。 Dashboard 有一个 UserListContainer,其中包含一个 UserList,其中列出了四个用户及其 ID 和姓名
我正在使用 React JS 构建一个应用程序,到目前为止我很喜欢它。但是,我遇到了一个错误,而且我很难确定我做错了什么。这是我的错误: 未捕获错误:不变违规:ImageList.render():必
我不明白这条消息想告诉我什么: CodeContracts: invariant unproven: _uiRoot != null && _uiRoot.Children != null && _u
我已经创建了 VenueList 组件。我想在 native 应用程序中使用 FlatList 组件显示列表。我收到错误消息:Invariant Violation 试图让帧超出范围索引(参见屏幕截图
尝试添加 onClick 时出现以下错误属性(property)给表格内的元素。这是我的错误: Invariant Violation: findComponentRoot(...) ... Unab
最初我的 mongod 进程运行良好。我发现我的虚拟机上有空间紧缩,所以删除了 2 个较旧的 oplog 文件以释放空间。然后接下来我启动 mongod 并开始出现错误。然后我尝试在本地数据库中修复我
我有一个Angular 11项目,试图集成SpreadJS Designer,但在ngcc步骤Compiling @grapecity/spread-sheets-designer-angular :
我正在寻找一种有条理的方法来追踪哪个组件在不熟悉的代码库中导致了“未捕获的不变违规”错误。特别是在运行不输出堆栈跟踪的业力测试套件时。 我最幸运的是将 console.log(JSON.stringi
设置状态导致第二次渲染后 react 测试失败 到目前为止,JSDOM 和 Mocha 的测试进展顺利。到目前为止,还没有必要测试任何改变其状态的组件。我发现我的第一个问题是测试更改其状态的组件。 错
这是一个典型的 react 问题,但我有点不知道如何处理它。我只想动态渲染我的表格线,但我收到错误:”“未捕获错误:不变违规:processUpdates():无法找到元素的子元素 2。这可能意味着
我知道错误来自嵌套的东西和嵌套_.map功能,但我不知道如何做到这一点。 渲染函数的一部分如下: 我想做的是: 检查是this.props.currencylist存在 然后显示下拉菜单 与 来自th
由于缺乏对 Commonjs 模块的支持,在对 React_rails gem 感到沮丧之后,我正在测试 react_webpack_rails来自 netguru 的 gem 。但从那时起我就遇到了
在 JEST 中为 React 文件编写测试用例时,我收到此错误。以下是我的示例代码: search_basr_test.js jest.autoMockOff(); global.React = r
我是一名优秀的程序员,十分优秀!