Reactjs ref不工作(react js)

Reactjs ref不工作(react js),reactjs,ref,Reactjs,Ref,嘿,我读了ref文件,并试图做同样的事情,但尽管有相同的行添加,但这不起作用: 从“React”导入React 导入“./Nav.css” 从“react router dom”导入{NavLink} 从“js cookie”导入cookie 从“./Logo”导入徽标 类Nav扩展了React.Component{ 构造器{ 超级作物 this.textInput=React.createRef;///I INIT MY REF//// 此.state={ 展示类别:假 } this.dis

嘿,我读了ref文件,并试图做同样的事情,但尽管有相同的行添加,但这不起作用:

从“React”导入React 导入“./Nav.css” 从“react router dom”导入{NavLink} 从“js cookie”导入cookie 从“./Logo”导入徽标 类Nav扩展了React.Component{ 构造器{ 超级作物 this.textInput=React.createRef;///I INIT MY REF//// 此.state={ 展示类别:假 } this.displayMenu=this.displayMenu.bindthis; this.closeMenu=this.closeMenu.bindthis; this.logOutKillCookie=this.logOutKillCookie.bindthis; } 显示菜单{ this.setState{showCategory:true},=>{ document.addEventListener'click',this.close菜单 } } 闭门事件{ console.logevent.target,this.dropdown菜单 如果!this.textInput.containsevent.target{///错误,因为this.textInput为NULL//// this.setState{showCategory:false},=>{ document.removeEventListener“单击”此.Close菜单 } } } 组件安装{ console.logthis.textInput///将我呈现为NULL//// } 注销KillCookie{ Cookies.删除'id' Cookies。删除“用户名” } 渲染{ 回来 {'\u2630'} {this.state.showCategory? ////我设置了REF值//// 火柴 聊天 Profil :null} ; } }
导出默认导航 没有ref,渲染条件的原因:

{(this.state.showCategory) ? 
初始状态showCategory=false,因此组件安装状态和ref上没有带ref的div为null。

您的状态中的showCategory值为false,因此带ref的div将永远不会呈现,因此它将为null。