I'm using React on Rails and currently logging out using a ERB component. I'm creating a hamburger menu for the app and putting the logout in it. Currently it just sitting out in the opening using <%= link_to "Logout", destroy_user_session_path, method: :delete %> in Rails. I would like to put it in the React component. Please help. I'm still new at using React on Rails. The code for this is below.
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
This^ is being imported to here(below). It works with the rest of the code.
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
I got the log out working using this code. Thank you all who responded.
import React, { Component } from 'react'
import axios from 'axios'
// import './menuContent.scss'
class MenuContent extends Component {
constructor(props) {
super(props)
}
handleLogout = () => {
const link = document.createElement('a');
link.setAttribute('href', '/users/sign_out');
link.setAttribute('rel', 'nofollow');
link.setAttribute('data-method', 'delete');
document.body.appendChild(link);
link.click();
}
render() {
return (
<div className="grandMenu">
<div className="menu">
<div className="signButton"></div>
<button onClick={this.handleLogout}>Sign Out</button>
<p>hello world</p>
</div>
<p className="hint">
Click outside the menu to close it, or swipe it away.
</p>
</div>
)
}
}
export default MenuContent
P.S. I should of mention this earlier. The log in is though a API and using Devise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With