reactjs - 条件渲染,使用超时 Invalid hook call React
问题描述
当检查为真时,我想显示下一步按钮。我收到意外令牌、无效挂钩调用等错误。请帮助我。在此先感谢。
import React from "react";
import useTimeout from "use-timeout";
class App extends React.Component {
state = { check: true };
handleCheck = () => {
this.setState({ check: !this.state.check });
};
render() {
useTimeout(() => {
this.handleCheck();
}, 10000);
return (
<div>
{
if(this.state.check){
return <button>Next</button>
}
}
</div>
);
}
}
export default App;
解决方案
改为这样做:
<div> {this.state.check && <button>Next</button> </div>
并删除useTimeout
你不需要它,你也不能使用它,因为它是一个钩子,你正在使用一个类组件。您应该onClick
改为触发它,或者如果您坚持使用超时使用setTimeout
,但我不建议使用该内部渲染
使用timeout
这样的:
componentDidmount() {
setTimeout(() => {
this.handleCheck();
}, 10000);
}
推荐阅读
- python-3.x - 我如何判断我的用户是否专注于终端?
- node.js - 将贝宝捕获和自定义订单保存到数据库
- jolt - 使用数组进行 JOLT 转换
- jenkins - 关于在jenkins中同时执行gradle项目的Spock集成测试的问题
- android - Facebook 受众网络竞价整合
- php - 插件 Joomla 3.x 登录后重定向 joomla 用户
- mysql - MySQL 错误:“where 子句”中的“1”中的未知列
- php - 使用 FORM 上传到 S3 存储桶的 AWS PHP 开发工具包
- python - Hvplot AttributeError:“列表”对象没有属性“xy”
- ubuntu - 无法启动铬浏览器,有什么解决办法吗?