React Native错误:属性“历史记录”不存在

War*_*ord 1 typescript reactjs react-router react-native

我正在使用 mongodb、express 和 React 创建注册登录模块,但在创建此模块时遇到属性历史记录不存在错误:

类型“Readonly & Readonly<{children?: ReactNode;”上不存在属性“history” }>'.ts(2339)

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

interface userprops{}
interface data{
    email:string;
    password:string;
    errors: string;
}
Run Code Online (Sandbox Code Playgroud)

//主类:

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
Run Code Online (Sandbox Code Playgroud)

错误信息 :

类型“Readonly & Readonly<{children?: ReactNode;”上不存在属性“history” }>'.ts(2339)

我已经尝试了很多方法,但这个错误不会消失

这应该运行

Jur*_*can 5

我猜你正在使用反应路由器

如果是这样,您可以使用路由器将传递给您的组件的确切类型来扩展您的接口

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

interface userprops extends RouteComponentProps {}
Run Code Online (Sandbox Code Playgroud)