0
class homeNotLoggedIn extends React.Component {
    componentDidMount() {
        function logout(){
            localStorage.clear()
            location.reload()
        }
    }

    render() {
        return (
            <div>
                <div className="nav_bar">
                    <button id='logout' onClick={ logout() }>

Im stuck here because I can't move the function out of componentDidMount because I need localStorage and the location property from the client, I know its going to say "logout is not defined" but I do not know how do I define it in this case

Onii-Chan
  • 57
  • 1
  • 6
  • See https://stackoverflow.com/a/59540455/11667949 – Shivam Jha Jul 21 '21 at 04:58
  • You can move the function to the class. The `onClick` event will always happen on the client-side where `window.localStorage` and `window.location` are set. Also, make sure to set the function as `onClick={this.logout}`. – juliomalves Jul 21 '21 at 20:33

1 Answers1

0

You should try to implement something similar to this. You can get the location from getInitialProps/getStaticProps/getServerSideProps and then pass it as a prop to your component.

ZealousWeb
  • 1,647
  • 1
  • 10
  • 11