score:8

Accepted answer

where did you define history here? there is a global window.history which is a web standard. if you want to use the react-router history, it's passed as a prop. try props.history.push() instead.

the component must receive the history prop from react router. so it should be a direct child of a route component, for example, or you must pass down props via its parents.

score:2

in react 16 and above you can use redirect from 'react-router-dom'. in your case you will get same outcomes if you use redirect instead history.

import  { redirect } from 'react-router-dom' 

define state in your component

this.state = {
  loginstatus:true
}

than in your render method

render () {
if(this.state.loginstatus){
    return <redirect to='/home'  />
 }
return(
 <div> please login </div>
 )
}

edit: using this.props.history

there are two things i found missing in your code. one is your login method binding. bind your method so that you can get access to this of class. other things is use withrouter hoc. withrouter will give you access to the this.history prop. like so below.

import react from "react";
import { withrouter } from "react-router";

class mainclass extends component {
  constructor(props) {
      this.login = this.login.bind(this)
   }

  login(){
      this.props.history.push('/new-location')
   }

   render(){
     return (
       <div classname="loginwrapper">
          <button onclick={this.login} classname="btn btn- 
          primary">pieteikties</button>
      </div>
      )  
    }

 export default withrouter(mainclass);

score:21

update: i found a new way to do this type of thing. same as b4 you need to install them types:

1.- npm i react-router react-router-dom

2.- npm i -d @types/react-router @types/react-router-dom

import react from "react";
import { routecomponentprops } from "react-router-dom";

interface mycomponentprops extends routecomponentprops {
 someofyourownprops: any;
 somemorepropsifneedit: any;
}

interface mycomponentstate {
  someproperty: any;
  another: any;
}

export class mycomponent extends react.component<mycomponentprops, mycomponentstate> {

    public state: mycomponentstate;
    public constructor (props: mycomponentprops) {
        super(props);

        this.state = {
            someproperty: "",
            another: ""
        }
    }

    public onclickhandler (evt: react.mouseevent<htmlbuttonelement, mouseevent>): void {
        evt.preventdefault();
    }

    public componentdidmount () {
        this.props.history;
    }
   public render (): react.reactelement<mycomponentprops> {
        return (
            <div>trol</div>
        )
    }
}

hihitl i know whats happening. hope you sill need it.

1.- npm i react-router react-router-dom

2.- npm i -d @types/react-router @types/react-router-dom

import react from "react";
import { history, locationstate } from "history";

interface mycomponentprops {
 someofyourownprops: any;
 history: history<locationstate>;
 somemorepropsifneedit: any;
}

then on your component if it is a class do

class mycomponent extends component<mycomponentprops, {}> {}

if it is a functional

const mycomponent = (props: mycomponentprops) => {}

Related Query

More Query from same tag