- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前是这个框架的新手,我对这个错误有疑问。为什么这个错误显示在我的控制台上。汉堡包图标显示在我的导航栏顶部。我用这个https://medium.com/zestgeek/ant-design-navbar-with-responsive-drawer-a8d718e471e0我的导航响应抽屉。我只是遵循他在每个组件上插入的代码,但我修改了其中的一些代码。我希望有人可以帮助我。
网站输出:
错误:
Warning: Can't call setState on a component that is not yet mounted. This is a no-op, but it might indicate a bug in your application. Instead, assign to `this.state` directly or define a `state = {};` class property with the desired state in the Navigation component.
导航 Js:
import React, { Component } from 'react';
import {NavLink} from 'react-router-dom';
import LeftMenu from './LeftMenu';
import RightMenu from './RightMenu';
import { Drawer, Button } from 'antd';
class Navigation extends Component {
constructor(props) {
super(props);
this.state = {
current: 'mail',
visible: false
};
this.showDrawer = this.showDrawer(this);
this.onClose = this.onClose(this);
}
showDrawer(){
this.setState({
visible: true,
});
}
onClose(){
this.setState({
visible: false,
});
}
render() {
return (
<nav className="menuBar">
<div className="logo">
<a href="">logo</a>
</div>
<div className="menuCon">
<div className="leftMenu">
<LeftMenu />
</div>
<div className="rightMenu">
<RightMenu />
</div>
<Button className="barsMenu" type="primary" onClick={this.showDrawer}>
<span className="barsBtn"></span>
</Button>
<Drawer
title="Basic Drawer"
placement="right"
closable={false}
onClose={this.onClose}
visible={this.state.visible}
>
<LeftMenu />
<RightMenu />
</Drawer>
</div>
</nav>
)
}
}
export default Navigation;
CSS:
<style>
.menuBar{
padding: 0 20px;
border-bottom: solid 1px #e8e8e8;
overflow: auto;
box-shadow: 0 0 30px #f3f1f1;
}
.logo{
width: 150px;
float: left;
}
.logo a{
display: inline-block;
font-size: 20px;
padding: 19px 20px;
text-transform: capitalize;
}
.menuCon{
width: calc(100% - 150px);
float: left;
}
.menuCon .ant-menu-item{
padding: 0px 5px;
}
.menuCon .ant-menu-submenu-title{
padding: 10px 20px;
}
.menuCon .ant-menu-item a, .menuCon .ant-menu-submenu-title a{
padding: 10px 15px;
}
.menuCon .ant-menu-horizontal{
border-bottom: none;
}
.menuCon .leftMenu{
float: left;
}
.menuCon .rightMenu{
float: right;
}
.barsMenu{
float: right;
height: 32px;
padding: 6px;
margin-top: 8px;
display: none;
background: none;
}
.barsBtn{
display: block;
width: 20px;
height: 2px;
background: #1890ff;
position: relative;
}
.barsBtn:after,.barsBtn:before{
content: attr(x);
width: 20px;
position: absolute;
top: -6px;
left: 0;
height: 2px;
background: #1890ff;
}
.barsBtn:after{
top: auto;
bottom: -6px;
}
.ant-drawer-body{
padding: 0;
}
.ant-drawer-body .ant-menu-horizontal > .ant-menu-item, .ant-drawer-body .ant-menu-horizontal > .ant-menu-submenu{
display: inline-block;
width: 100%;
}
.ant-drawer-body .ant-menu-horizontal{
border-bottom: none;
}
.ant-drawer-body .ant-menu-horizontal > .ant-menu-item:hover{
border-bottom-color: transparent;
}
@media(max-width: 767px){
.barsMenu{
display: inline-block;
}
.leftMenu,.rightMenu{
display: none;
}
.logo a{
margin-left: -20px;
}
.menuCon .ant-menu-item, .menuCon .ant-menu-submenu-title{
padding: 1px 20px;
}
.logo a{
padding: 10px 20px;
}
}
</style>
最佳答案
为自动绑定(bind)的函数使用粗箭头
showDrawer = () => {
this.setState({
visible: true,
});
}
onClose = () => {
this.setState({
visible: false,
});
}
并从构造函数中删除以下行
this.showDrawer = this.showDrawer(this);
this.onClose = this.onClose(this);
或
constructor(props) {
super(props);
this.state = {
current: 'mail',
visible: false
};
this.showDrawer = this.showDrawer.bind(this);
this.onClose = this.onClose.bind(this);
}
如果你想在状态改变时隐藏按钮
<Button className={`barsMenu {!this.state.visible ? 'hideBarsMenu' : ''`} type="primary" onClick={this.showDrawer}>
<span className="barsBtn"></span>
</Button>
并添加一个CSS类
.hideBarsMenu {
display: none;
}
关于javascript - 如何修复无法在组件上调用 setState,但它可能会在使用 React Js 的应用程序中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57087427/
我可以在传递给 this.setState() 的回调定义中显式调用 this.setState() 吗? this.setState( { openA:true }, () => {
我有一个从 api 获取数据的场景。在这种情况下,每当我从商店获取新值时,我的 componentWillReceiveProps() 就会被触发。 componentWillReceiveProps
在这个 componentDidUpdate 方法中,在执行 setState 将引号设置为从 fetch 返回的内容后,我必须使用回调再次执行 setState 以将 randomQuoteInde
正如 another question 中向我指出的那样,如果我在 setState、randomQuoteIndex() 中有一个不带参数的函数调用,并且该函数使用该 setState 中设置的状态
问题:当我使用 this.setState 并在回调中输出状态时,它根本不会改变,但是当我将 setstate 嵌套在 setstate 中时,它就会正常工作。 例子:这行不通- this.setSt
这个问题在这里已经有了答案: Why does setState take a closure? (2 个答案) 关闭 4 年前。 我对这两者的区别还是有点迷惑 isBusy = false;
当我使用 setState () 文本出现在调试控制台中.. setState() callback argument returned a Future. The setState() method
这个问题已经有答案了: Are 'Arrow Functions' and 'Functions' equivalent / interchangeable? (4 个回答) React - unca
之间的区别 - this.setState({value: 'xyz', name: 'john', color: 'orange'}) 对比 setValue('xyz'); setName('jo
之间的区别 - this.setState({value: 'xyz', name: 'john', color: 'orange'}) 对比 setValue('xyz'); setName('jo
我在运行代码时收到以下警告: Line 48: Do not mutate state directly. Use setState() react/no-direct-mutation-state
我是 React 的新手,我注意到我们使用 this.setState() 而不是 super.setState() 请我清楚地解释为什么我们用它来调用父类(super class)方法??示例: c
我一生都无法在我的 react 组件中传递这个 TypeError 。我已阅读所有相关主题并实现了几乎所有建议的修复,但均无济于事。 import React, { Component } from
我知道这可能是一个 JavaScript 问题而不是 React 问题,但我无法理解 React setState 的上述签名。 函数参数列表中的方括号和逗号有什么作用? 我知道如何将它与更新程序一起
是否可以在this.setState的回调中调用this.setState? 我正在制作一个 Roguelike Dungeon 并有一个设置,其中在 this.setState 的回调中使用了一个辅
我正在使用 react-navigation 进行路由。在一个组件上,我尝试设置 navigationOptions 以显示汉堡包按钮以打开和关闭侧边栏(抽屉)。所以 onPress 我试图为我的侧边
在之前的 React 版本中,我们可以在状态更改后执行代码,方法如下: setState( prevState => {myval: !prevState.myval}, () => { co
有一个 react 问卷组件,它将选定的答案添加到 this.state = {answer: []} 中,效果很好。同时,当用户更新答案时,它会添加为另一个对象。当questionID相同时,是否有
我正在使用 WebSocket 与我的服务器进行通信,并在我的 handleSubmit() 函数上输入一些值,并在此基础上与服务器进行通信并将我的状态更新为数据从 ws 收到。所以,第一次,一切都工
componentDidMount(prevProps, prevState, prevContext) { let [audioNode, songLen] = [this.refs.aud
我是一名优秀的程序员,十分优秀!