您好,我在用户点击汉堡菜单时使用React创建侧边栏,问题是当用户单击汉堡菜单时,侧边栏出现但导航栏消失了,我该如何渲染导航栏和侧边栏不会互相覆盖?
var app = document.getElementById('app');
class Navbar extends React.Component {
constructor(props) {
super(props);
this.state = {
sidebarOn: false
};
this.onClickOpen = this.onClickOpen.bind(this);
}
onClickOpen(open) {
console.log(open);
if(open) {
this.setState({sidebarOn: open});
Menu();
}
}
render() {
return(
<div>
<header>
<nav class="navbar navbar-custom sticky-top">
<a class="navbar-brand" href="#">TO DO LIST</a>
<a class="burger-menu" href="#"><img src='/images/hamburger-icon.png' onClick={() => this.onClickOpen(true)}></img></a>
</nav>
</header>
</div>
);
}
}
function Menu() {
class Parent extends React.Component {
constructor() {
super();
this.state = {
burgerOn: "burger-right"
};
}
render() {
return (
<div class={this.state.burgerOn}>
<div class="container">
<form >
<div class="form-group task-title">
<label for="title">
Task Title
</label>
<input type="title" class="form-control" placeholder="Enter Task Title"></input>
</div>
<div class="form-group task-desc">
<label for="desc">Task Description</label>
<textarea type="text" class="form-control" id="desc" placeholder="Enter Task Description" rows="5"></textarea>
</div>
</form>
<div>
<button type="button" class=" task-btn btn btn-primary">Create</button>
</div>
</div>
</div>
);
}
}
ReactDOM.render(<Parent />, app);
}
ReactDOM.render(<Navbar />, app);
var app = document.getElementById('app');
class Navbar extends React.Component {
constructor(props) {
super(props);
this.state = {
sidebarOn: false
};
this.onClickOpen = this.onClickOpen.bind(this);
}
onClickOpen(open) {
console.log(open);
if(open) {
this.setState({sidebarOn: open});
Menu();
}
}
render() {
return(
<div>
<header>
<nav class="navbar navbar-custom sticky-top">
<a class="navbar-brand" href="#">TO DO LIST</a>
<a class="burger-menu" href="#"><img src='/images/hamburger-icon.png' onClick={() => this.onClickOpen(true)}></img></a>
</nav>
</header>
</div>
);
}
}
function Menu() {
class Parent extends React.Component {
constructor() {
super();
this.state = {
burgerOn: "burger-right"
};
}
render() {
return (
<div class={this.state.burgerOn}>
<div class="container">
<form >
<div class="form-group task-title">
<label for="title">
Task Title
</label>
<input type="title" class="form-control" placeholder="Enter Task Title"></input>
</div>
<div class="form-group task-desc">
<label for="desc">Task Description</label>
<textarea type="text" class="form-control" id="desc" placeholder="Enter Task Description" rows="5"></textarea>
</div>
</form>
<div>
<button type="button" class=" task-btn btn btn-primary">Create</button>
</div>
</div>
</div>
);
}
}
ReactDOM.render(<Parent />, app);
}
ReactDOM.render(<Navbar />, app);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="text/babel" src="/js/app.js"></script>
</body>
</html>
如片断所示,如果单击断开的图像链接,我的表单将覆盖待办事项导航栏。
非常感谢您的建设性帮助:)
答案 0 :(得分:1)
问题是,当您要渲染侧边栏菜单时,您正在使用此代码
ReactDOM.render(<Parent />, app);
这意味着只需在html文件中的app
元素内插入组件即可。结果就像清除先前渲染的组件,然后渲染新的组件。所以这就是您在这里看到的:
...some code
}
}
ReactDOM.render(<Parent />, app); <---- here
}
ReactDOM.render(<Navbar />, app); <---- and here
... morecode
您不需要直接在根元素内部渲染刚创建的每个组件的内容。 您只需要在其中渲染最高级别的组件 所有其他组件都应嵌套在父组件中,这样,您将只在html文件的根元素内呈现一个父组件,而通过将其导入内部,其他所有组件也将包含在其中您的父组件并在那里使用它们,如下所示:
import React from 'react'
import ReactDOM from 'react-dom'
import B from './B'
class A extends React.component {
render() {
return(
<div>
<B />
</div>
)
}
}
ReactDOM.render(
<A />,
document.querySelector('#app')
)
答案 1 :(得分:0)
我能够在a_m_dev的帮助下解决我的问题,然后添加了条件语句来检查该类是否打开,就像这样:
char *c=malloc(SIZE);
char *p=malloc(SIZE);
我希望这对遇到同样问题的人有所帮助:)