0
林試圖建立從這個例子的切換右鍵菜單:how-to-build-a-sliding-menu-using-react-js"陣營切換菜單
問題React.createClass已被棄用,所以我不得不改變代碼,並停止工作,我得到的內容,但不能訪問處理程序cann有人告訴我,我應該採取哪些步驟來解決這個問題!所以,如果我在我的按鈕點擊我得到這個錯誤:
Uncaught TypeError: Cannot read property 'show' of undefined
ToggleMenu
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import localStyles from './ToggleMenu.scss';
import { themr } from 'react-css-themr';
import { Menu } from '../../components';
import { MenuItem } from '../../components';
@themr('ToggleMenu', localStyles)
export default class ToggleMenu extends React.Component {
showRight() {
this.refs.right.show();
}
constructor(props) {
super(props);
this.showRight = this.showRight.bind(this);
}
render() {
return (
<div>
<button onClick={this.showRight}>Show Right Menu!</button>
<Menu ref={right => this.right = right} alignment="right">
<MenuItem hash="first-page">First Page</MenuItem>
<MenuItem hash="second-page">Second Page</MenuItem>
<MenuItem hash="third-page">Third Page</MenuItem>
</Menu>
</div>
);
}
}
菜單
import React from 'react';
export default class Menu extends React.Component {
constructor() {
super();
this.state = {
visible: false
}
};
show() {
this.setState({visible: true});
document.addEventListener("click", this.hide.bind(this));
}
hide() {
this.setState({visible: false});
document.removeEventListener("click", this.hide.bind(this));
}
render() {
return (
<div className="menu">
<div className={(this.state.visible ? "visible " : "") + this.props.alignment}>{this.props.children}</div>
</div>
);
}
}
菜單項
import React from 'react';
export default class MenuItem extends React.Component {
navigate(hash) {
window.location.hash = hash;
}
render() {
return (
<div className="menu-item" onClick={this.navigate.bind(this, this.props.hash)}>{this.props.children}</div>
);
}
}
確定錯誤已經不顯示,但是,現在沒有什麼happend的菜單仍然在同一個地方:/ UR,但工作的解決方案,它跳進播放功能:) – Alex
大概CSS?整個'Menu'類看起來奇怪,這不是反應方式;)我會更新我的答案給你出個主意 – Crysfel
這將是一個簡單的第一個正確的解決方案切換菜單反應:d整個網站:d發現剛準備開發npm包裝。非常感謝你 – Alex