gpt4 book ai didi

html - 点击打开reactjs sideNav

转载 作者:太空宇宙 更新时间:2023-11-04 01:56:27 24 4
gpt4 key购买 nike

我试图在点击按钮时打开react sideNav栏。我指的是这些文章来做到这一点:

https://github.com/gauravchl/react-simple-sidenav

https://www.npmjs.com/package/react-simple-sidenav

https://gauravchl.github.io/react-simple-sidenav/demo/

我已经正确地实现了它,但我仍然坚持在单击 button 时打开 sideNav

<div>
<MenuIcon onClick={() => this.setState({showNav: true})}/>
<SideNav
title="Simple Sidenav"
items={['Item 1', 'Item 2']}
openFromRight="true"
showNav = {this.state.showNav}
/>
</div>

如果我给 showNav = 'true'sideNav 在页面打开时默认打开。如果我给 showNav = {this.state.showNav} 我得到一个错误:

Warning: validateDOMNesting(...): <html> cannot appear as a child of <div>. See div > App > html.
printWarning @ bundle.js:11845
bundle.js:35792 Uncaught TypeError: Cannot read property 'showNav' of null
at Notification.render (bundle.js:35792)
at bundle.js:26635
at measureLifeCyclePerf (bundle.js:25914)
at ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext (bundle.js:26634)
at ReactCompositeComponentWrapper._renderValidatedComponent (bundle.js:26661)
at ReactCompositeComponentWrapper.performInitialMount (bundle.js:26201)
at ReactCompositeComponentWrapper.mountComponent (bundle.js:26097)
at Object.mountComponent (bundle.js:18478)
at ReactDOMComponent.mountChildren (bundle.js:25215)
at ReactDOMComponent._createInitialChildren (bundle.js:22220)

最佳答案

检查了github链接,在描述中他们没有定义showNav变量的初始状态,你需要在你的组件中添加这部分,来定义它将起作用的初始值:

getInitialState : function() {
return {
showNav: false,
};
},

如果你查看元素文件demo/src/layout.js,定义了showNav,在描述中他们没有写一个工作代码,主要集中在如何使用示例代码 SideNav

更新:

import react from 'react';
import SideNav, {MenuIcon} from 'react-simple-sidenav';

React.createClass({

getInitialState : function() {
return {
showNav: false,
};
},

render() {
.....
}
})

关于html - 点击打开reactjs sideNav,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42524791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com