简体   繁体   中英

React Native Error : Property 'history' does not exist

I am creating the signup sign-in module using mongodb, express and react but while creating this I encountered with property history does not exist error:

Property 'history' does not exist on type 'Readonly & Readonly<{ children?: ReactNode; }>'.ts(2339)

import React, { Component } from 'react'
import { login } from './userfunctions'

interface userprops{}
interface data{
    email:string;
    password:string;
    errors: string;
}

//main class:

class Login extends Component<userprops, data> {
  constructor(props: userprops) {
    super(props)
    this.state = {
      email: '',
      password:'',
      errors: ''
    }
this.onChange = this.onChange.bind(this) //binding the function
this.onSubmit = this.onSubmit.bind(this)  //binding the function
  }
  onChange(e:any) {  //on change function
    this.setState({ email: e.target.value })
  }
  onSubmit(e:any) {   //on submitting the form in render function this function will fire
e.preventDefault()
const user = {
email: this.state.email,
      password: this.state.password
    }


    login(user).then(res => { //I had created login function in another component
      if (res) {
         this.props.history.push(`/profile`)  //this line is giving error
      }
    })

  }
render() {
   //rendering data
}

export default Login

error message :

Property 'history' does not exist on type 'Readonly & Readonly<{ children?: ReactNode; }>'.ts(2339)

I have tried a lot of things but this error won't go

this should run

i guess you are using react router

if so you can extend your interface with exact type what router will pass to your component

import { RouteComponentProps } from 'react-router-dom';

interface userprops extends RouteComponentProps {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM