我一直在尝试建立一个reactjs网站,但显示无法编译。我收到很少的错误,指出这些状态和功能中有很少的未定义,这可能是解决方案。我粘贴了下面的代码。 我的代码所做的是,您输入标题和描述,然后单击“保存”按钮时,它将保存帖子。我尝试将snippetDescription绑定到保存功能,但所有内容均未定义。
import React, { Component, useState } from "react";
import { Link } from "react-router-dom";
import { Button } from "react-bootstrap";
handleRequest = async (postId) => { //passed postId here
const post = this.state;
const request = {
method: "POST",
headers: {
"Content-Type": "application/json"
}
};
if (postId != null) {
post["id"] = postId;
}
try {
const response = await fetch("/api/updateposts", {
...request,
body: JSON.stringify(this.state)
});
const data = await response.json();
if (data.status === 200) {
console.log("success", "post saved successfully!");
} else {
console.log(
"danger",
"An error has occured while updating the post. Please try again"
);
}
} catch (ex) {
console.error(ex.stack);
console.log(
"danger",
"An error has occured while updating the post. Please try again"
);
}
};
handlePost = (postId) => { //passed postId here
if (postId == null) {
return handleRequest("/api/savepost");
}
return handleRequest("/api/updatepost");
};
const analysis = (snippetDescription) => { //passed snippetDescription
fetch("/api/analyse", {
method: "POST",
body: JSON.stringify({
snippetdesc: "snippetDescription"
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(
textdata => {
this.setState({
textdata: textdata.data,
textlen: snippetDescription.split(" ").length
});
},
error => {
console.log(error);
}
);
};
export default class MainText extends Component {
constructor(props) {
super(props);
this.state = {
title: "",
description: "",
id: null,
snippetDescription: "",
textlen: 0
};
}
render() {
return (
<>
<input
type="text"
id="titletext"
placeholder="Enter title here"
limit-to="64"
className="inptxt"
onChange={title => this.setState({ title })}
></input>
<span class="itemcenter">
<Link to="/about">
<Button
className="btn savebtn"
onClick={() => handlePost({ ...this.state })}
>
Save <i className="fas fa-save" />
</Button>
</Link>
</span>
<textarea
class="textareaclass"
placeholder="Enter your text here"
onChange={snippetDescription =>
this.setState({ snippetDescription })
}
></textarea>
</>
)}
这是我收到的错误消息
Failed to compile.
./src/component/Text.js
Line 5: 'handleRequest' is not defined no-undef
Line 15: 'postId' is not defined no-undef
Line 16: 'postId' is not defined no-undef
Line 43: 'handlePost' is not defined no-undef
Line 44: 'postId' is not defined no-undef
Line 45: 'handleRequest' is not defined no-undef
Line 47: 'handleRequest' is not defined no-undef
Line 119: 'snippetDescription' is not defined no-undef
Line 287: 'handlePost' is not defined no-undef
我的状态没有传递给功能吗?我的handleRequest和handlePost函数也没有定义吗?
更新: 我已经将所有代码(函数)移到了react组件中,添加了this.handlePost,它仍然显示相同的错误。
./src/component/mainText.js
Line 31: 'postId' is not defined no-undef
Line 32: 'postId' is not defined no-undef
Line 59: 'postId' is not defined no-undef
Line 60: 'handleRequest' is not defined no-undef
Line 62: 'handleRequest' is not defined no-undef
Line 134: 'snippetDescription' is not defined no-undef
新更新:
我现在已经编辑了函数,并将值传递给了我在上面的代码中未曾传递过的函数,所以现在我遇到的唯一错误是未定义handleRequest。我应该在某个地方声明handleRequest吗?
Line 60: 'handleRequest' is not defined no-undef
Line 62: 'handleRequest' is not defined no-undef
Updae:我已经添加了this.handlerequest.bind(this),所以它不再显示错误了。我希望绑定是正确的。有人可以验证
handlePost = postId => {
if (postId == null) {
return this.handleRequest("/api/savepost").bind(this);
}
return this.handleRequest("/api/updatepost").bind(this);
};
答案 0 :(得分:0)
将所有相关代码错误地移到React Component的范围内。您不能在React组件类中声明和使用函数并声明状态。 在组件的事件处理程序中,必须使用“ this”调用组件函数。上下文。例如:
onClick={() => this.handlePost....}