【问题标题】:React Native Error : Property 'history' does not existReact Native 错误:属性“历史”不存在
【发布时间】:2019-11-18 22:53:13
【问题描述】:

我正在使用 mongodb 创建注册登录模块,表达和反应,但是在创建这个时我遇到了属性历史不存在错误:

“只读”和“只读”类型上不存在属性“历史”'.ts(2339)

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

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

//主类:

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

错误信息:

“只读”和“只读”类型上不存在属性“历史”'.ts(2339)

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

这应该运行

【问题讨论】:

  • 你的userprops界面中没有history
  • 非常感谢。通过添加界面 userprops{history:any} 解决了我的问题

标签: reactjs typescript react-native react-router


【解决方案1】:

我猜你正在使用 react 路由器

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

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

interface userprops extends RouteComponentProps {}

【讨论】:

    猜你喜欢
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2021-11-16
    相关资源
    最近更新 更多