我正在使用 React on Rails,目前正在使用 ERB 组件注销。我正在为应用程序创建一个汉堡菜单并将注销放入其中。目前它只是<%= link_to "Logout", destroy_user_session_path, method: :delete %>
在 Rails 中使用。我想把它放在 React 组件中。请帮忙。我对使用 React on Rails 还是很陌生。代码如下。
import React, { Component } from 'react'
class MenuContent extends Component {
constructor(props) {
super(props)
}
render() {
return (
<div className="menu">
<div className="Logout">
I'm trying to add the JavaScript code here. Here how I'm doing it in Ruby.
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
</div>
<p className="hint">Click outside the menu to close it, or swipe it closed on touch device</p>
</div>
)
}
}
export default MenuContent
这个^ 被导入到这里(下)。它适用于其余代码。
import React, {Component} from 'react'
import CheeseburgerMenu from "cheeseburger-menu";
import HamburgerMenu from "react-hamburger-menu";
import MenuContent from "./MenuContent";
class Navbar extends Component {
constructor(props) {
super(props);
this.state = {
menuOpen: false
};
}
openMenu() {
this.setState({ menuOpen: true });
}
closeMenu() {
this.setState({ menuOpen: false });
}
render(){
return(
<div>
<CheeseburgerMenu
isOpen={this.state.menuOpen}
closeCallback={this.closeMenu.bind(this)}>
<MenuContent closeCallback={this.closeMenu.bind(this)} />
</CheeseburgerMenu>
<HamburgerMenu
isOpen={this.state.menuOpen}
menuClicked={this.openMenu.bind(this)}
width={32}
height={24}
strokeWidth={3}
rotate={0}
color="black"
borderRadius={0}
animationDuration={0.5} />
</div>
)
}
}
export default Navbar