class App extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({value: event.target.value});
}
handleSubmit(event) {
alert('A name was submitted: ' + this.state.value); //error here
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" value={this.state.value} onChange={this.handleChange} /> // error here
</label>
<input type="submit" value="Submit" />
</form>
);
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,我试图学习ReactJS,因为我需要做一个工作测试的网站,但我面临一些问题.我使用create-react-app工具,我需要创建一个表单,根据API的返回显示一些内容,但我只是在创建表单时面临问题,我在这里写的代码,我得到了以下错误:
error TS2339: Property 'value' does not exist on type 'Readonly<{}>'.
Run Code Online (Sandbox Code Playgroud)
我在代码中评论的两行中得到了这个错误.这个代码甚至不是我的,我从反应官方网站(https://reactjs.org/docs/forms.html)得到它,但它不在这里工作,我的目标是调整代码,但它不起作用,有人请帮我
我有一个非常基本的与tsx反应的程序,我收到一个错误,我无法弄清楚为什么
import React from 'react';
// import {connect} from 'react-redux'
export class Home extends React.Component {
render(){
console.log(this.props)
return (
<div>Working</div>
)
}
}
import * as React from 'react'
import * as ReactDOM from 'react-dom';
import {Home} from './Components/Home.component'
class App extends React.Component<any,any>{
render(){
return(
<Home value="abc" />
)
}
}
ReactDOM.render( <App />, window.document.getElementById("app"))
Run Code Online (Sandbox Code Playgroud)
git克隆这个代码
我有这个.tsx文件
import React, { Component } from 'react';
export class SidebarItem extends Component {
constructor (props) {
super(props);
}
render () {
return (<li>{this.props.children}</li>);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,TypeScript会抛出此错误:
error TS2339: Property 'props' does not exist on type 'SidebarItem'.
在尝试将文件转换为打字稿时,我收到上述错误
handleToggle() {
const { toggleTodo, id } = this.props; // <-- Error
toggleTodo(id);
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了几种方法,例如向我为此组件添加的接口添加属性、添加绑定语句、为这些属性添加方法等。但是它们都会给出错误,通常是上述错误。只是添加打字稿检查似乎我不需要做很多改变。我在这里看到的其他类似问题和答案没有帮助。例如,我将这些项目添加到界面道具中,但仍然出现错误。
到目前为止我的代码(在转换之前工作正常)是
import React, { Component } from 'react'
interface TodoItemProps { // Added this interface for props
title: string,
completed: boolean,
}
export default class TodoItem extends Component {
constructor(props:TodoItemProps) { // used inteface from abovereact
super(props);
this.handleToggle = this.handleToggle.bind(this);
this.handleRemove = this.handleRemove.bind(this);
}
handleToggle() {
const { toggleTodo, id } = this.props; // < -- errors here.
toggleTodo(id);
}
handleRemove() {
const { …Run Code Online (Sandbox Code Playgroud)