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)得到它,但它不在这里工作,我的目标是调整代码,但它不起作用,有人请帮我
修正:我在全球范围内卸载了VScode,打字稿和eslint。一旦我重新安装了VScode,一切都会恢复正常。
更新:安装@ types / react库后,我仍然仅对RecipeList.js和Recipe.js都得到类似的错误,如属性“ props”中提到的在 类型“ Home”上不存在或TypeScript属性“ props”不存在:
属性'recipes'在类型'Readonly <{children?:ReactNode;上不存在。}>和只读<{}>'。
原文:所以我是React的新手,由于某种原因,在VSCode上,尝试访问RecipeList.js和Recipe.js的.props时遇到语法错误。
这是Recipe.js的代码示例:
import React, {Component} from 'react';
import "./Recipe.css";
class Recipe extends Component {
// props: any; uncommenting this will fix the bug
render() {
// don't have to use return and parentheses for arrow with JSX
const ingredients = this.props.ingredients.map((ing, ind) => (
<li key={ind}>{ing}</li>
));
const {title, img, instructions} = this.props
return (
<div className="recipe-card">
<div className="recipe-card-img">
<img src={img} alt={title}/> …Run Code Online (Sandbox Code Playgroud)