按照tutorial但显然不能正常工作,按下按钮会出现右侧菜单但是出现这种情况,出了什么问题?
MenuContainer类将其更改为SideBar.js
所有课程都在 SideBar.js
内import React, { Component } from "react";
class SideBar extends React.Component {
constructor(props) {
super(props);
this.state = { visible: false };
this.handleMouseDown = this.handleMouseDown.bind(this);
this.toggleMenu = this.toggleMenu.bind(this);
}
toggleMenu() {
this.setState({
visible: !this.state.visible
});
}
handleMouseDown(e) {
this.toggleMenu();
console.log("clicked");
e.stopPropagation();
}
render() {
return (
<div>
<MenuButton handleMouseDown={this.handleMouseDown}/>
<Menu handleMouseDown={this.handleMouseDown} menuVisibility={this.state.visible}/>
<div>
<div>
<p>Can you spot the item that doesn't belong?</p>
<ul>
<li>Lorem</li>
<li>Ipsum</li>
<li>Dolor</li>
<li>Sit</li>
<li>Bumblebees</li>
<li>Aenean</li>
<li>Consectetur</li>
</ul>
</div>
</div>
</div>
);
}
}
export default SideBar;
class MenuButton extends React.Component {
render() {
return (
<button className="roundButton"
onMouseDown={this.props.handleMouseDown}></button>
);
}
}
class Menu extends React.Component {
render() {
var visibility = "hide";
if (this.props.menuVisibility) {
visibility = "show";
}
return (
<div className="flyoutMenu"
onMouseDown={this.props.handleMouseDown}
className={visibility}>
<h2><a href="#">Home</a></h2>
<h2><a href="#">About</a></h2>
<h2><a href="#">Contact</a></h2>
<h2><a href="#">Search</a></h2>
</div>
);
}
}
和我的 styles.scss
.roundButton {
background-color: #96D9FF;
margin-bottom: 20px;
width: 50px;
height: 50px;
border-radius: 50%;
border: 10px solid #0065A6;
outline: none;
transition: all .2s cubic-bezier(0, 1.26, .8, 1.28);
}
.roundButton:hover {
background-color: #96D9FF;
cursor: pointer;
border-color: #003557;
transform: scale(1.2, 1.2);
}
.roundButton:active {
border-color: #003557;
background-color: #FFF;
}
.flyoutMenu {
width: 100vw;
height: 100vh;
background-color: #FFE600;
position: fixed;
top: 0;
left: 0;
transition: transform .3s
cubic-bezier(0, .52, 0, 1);
overflow: scroll;
z-index: 1000;
}
.flyoutMenu.hide {
transform: translate3d(-100vw, 0, 0);
}
.flyoutMenu.show {
transform: translate3d(0vw, 0, 0);
overflow: hidden;
}
这是单击它时显示方式的图像。
答案 0 :(得分:0)
你从SideBar组件变异状态。边栏组件将状态作为道具向下传递到Menu。当您改变此状态时,它应该使用新值重新呈现Menu组件。
您的菜单类看起来更像是:
class Menu extends React.Component {
render() {
const { menuVisibility } = this.props
return (
<div className="flyoutMenu"
onMouseDown={this.props.handleMouseDown}
className={menuVisibility ? 'show' : 'hide'}>
<h2><a href="#">Home</a></h2>
<h2><a href="#">About</a></h2>
<h2><a href="#">Contact</a></h2>
<h2><a href="#">Search</a></h2>
</div>
);
}