gpt4 book ai didi

javascript - 仅在第二次调用函数时 react this.setState 不是一个函数

转载 作者:行者123 更新时间:2023-12-01 03:11:26 25 4
gpt4 key购买 nike

我对 React 很陌生,并且一直在制作 CRUD 应用程序来创建读取更新和删除命令。在制作弹出框时,将用于更新特定命令我遇到了错误: this.setState 不是函数。该错误仅在第二次调用 ChangeUpdateObject 函数时出现,该函数更改了弹出窗口中正在更新的命令。第一次打开弹出窗口时,会选择正确的命令,并且其信息会显示在弹出窗口中。我希望这足以解决问题,但如果有必要我可以发布更多内容。以下是 ChangeUpdateObject 所在组件的代码。

import React, { Component } from 'react';
import Commands from "./components/Commands";
import Header from "./components/Header";
import NewCommand from "./components/NewCommand";
import UpdateCommand from "./components/UpdateCommand";
import './App.css';
class App extends Component {
constructor(){
super();
this.state=({
//current command that is going to be sent to handleAddCommand
currentCom:[],
//CurrentCom's arguments
currentArgs:[],
//Output of the APICall()
output:[],
//Command Arguments of the Update POPUP
UpdateArguments:[],
//Selected Command that is being Updated in the UPDATE POPUP
selectedUpdateObject:{},
//Actual commands that are the responses of the command being UPDATED
selectedUpdateResponses:[]
});
this.APICall=this.APICall.bind(this);
this.changeUpdateObject=this.changeUpdateObject.bind(this);
this.handleAddArgument=this.handleAddArgument.bind(this);
this.handleAddUpdateArgument=this.handleAddUpdateArgument.bind(this);
this.APICall();
}
APICall(){
//Gets all commands from API
var request = new XMLHttpRequest();

request.open('GET', 'https://private-aa937-commandtable.apiary-mock.com/commands');

request.setRequestHeader('x-api-version', '1');

request.onreadystatechange = function () {
if (request.readyState === 4) {
var json = JSON.parse(request.responseText);
this.setState({
output:json
});
}
}.bind(this);
request.send();
}
handleAddCommand(command){
//Sends the command to the API
//needs work for formating the data to be sent
//So the data being sent isnt the same as data in API yet, need to be changed
let fullCommand=command;
let currentArgs=this.state.currentArgs;
delete currentArgs.id;
fullCommand.Arguments=currentArgs;
fullCommand= JSON.stringify(fullCommand)
console.log(fullCommand);

var request = new XMLHttpRequest();

request.open('POST', 'https://private-aa937-commandtable.apiary-mock.com/commands');

request.setRequestHeader('Content-type', 'application/json');
request.setRequestHeader('x-api-version', '1.0');

request.onreadystatechange = function () {
if (this.readyState === 4) {
console.log('Status:', this.status);
console.log('Headers:', this.getAllResponseHeaders());
console.log('Body:', this.responseText);
}
};

var body = fullCommand;

request.send(body);
}
handleAddArgument(Argument){
//Adds an argument to the NewCommand tab
let Arguments= this.state.currentArgs;
Arguments.push(Argument);
this.setState({currentArgs:Arguments});
}
handleAddUpdateArgument(Argument){
let Arguments= this.state.UpdateArguments;
//console.log("this is Argument :" + Argument +". This is Arguments :"+Argument.Name);
Arguments.push(Argument);
console.log(Arguments);
this.setState({
UpdateArguments:[]
});
}
handleDeleteArgument(id){
//Deletes an argument in the New Command Tab
let Arguments= this.state.currentArgs;
let index= Arguments.findIndex(x=> x.Id===id)
Arguments.splice(index,1);
this.setState({currentArgs:Arguments});
}
handleDeleteUpdateArgument(id){
//Deletes an argument in the Update POPUP
let Arguments= this.state.UpdateArguments;
let index= Arguments.findIndex(x=> x.Id===id)
Arguments.splice(index,1);
this.setState({UpdateArguments:Arguments});
}
handleUpArgument(id){
//Increases the position in the Arguments component in the New Command Tab
console.log("tried to up");
let Arguments=this.state.currentArgs;
let index=Arguments.findIndex(x=>x.Id==id);

if(index>0){
let placeholder1=Arguments[index-1];
let placeholder2=Arguments[index];
Arguments[index]=placeholder1;
Arguments[index-1]=placeholder2
this.setState({
currentArgs:Arguments
})
}
}
handleUpUpdateArgument(id){
//Increases the position in the Arguments component of the Update POPup
console.log("tried to up");
let Arguments=this.state.UpdateArguments;
let index=Arguments.findIndex(x=>x.Id==id);

if(index>0){
let placeholder1=Arguments[index-1];
let placeholder2=Arguments[index];
Arguments[index]=placeholder1;
Arguments[index-1]=placeholder2
this.setState({
UpdateArguments:Arguments
})
}
}
handleDownArgument(id){
// Decreases position of an argument in the Arguments component of the New Command tab
let Arguments=this.state.currentArgs;
let index=Arguments.findIndex(x=>x.Id==id);
if(index<(Arguments.length-1)){
let placeholder1=Arguments[index+1];
let placeholder2=Arguments[index];
Arguments[index]=placeholder1;
Arguments[index+1]=placeholder2
this.setState({
currentArgs:Arguments
})
}
}
handleDownUpdateArgument(id){
// Decreases position of an argument in the Arguments component of the Update POPUP
let Arguments=this.state.UpdateArguments;
let index=Arguments.findIndex(x=>x.Id==id);
if(index<(Arguments.length-1)){
let placeholder1=Arguments[index+1];
let placeholder2=Arguments[index];
Arguments[index]=placeholder1;
Arguments[index+1]=placeholder2
this.setState({
UpdateArguments:Arguments
})
}
}
changeUpdateObject(id){
//Changes the Command that is being updated in the Update popup
let UpdateObject=this.state.output[id-1];
this.setState({
selectedUpdateObject:UpdateObject
},()=>{
console.log("I think it updated it");
this.show("UpdateCommand");
});
}
SendResponses(Response,Arguments){
//Sends the commands that are used as responses for the command being UPDATED in the Updtate POPUP
//Also sends the arguments to the UPDATE POPUP so they can be displayed, seems to not be working.
console.log(Arguments);
this.setState=({
selectedUpdateResponses:Response,
UpdateArguments:Arguments
});
}
show(id){
//Shows the Update POPUP
document.getElementById(id).style.display="block";
}
render() {
return (
<div>
<UpdateCommand input={this.state.output}
Arguments={this.state.UpdateArguments}
Command={this.state.selectedUpdateObject}
addCommand={this.handleAddCommand.bind(this)}
addArgument={this.handleAddUpdateArgument.bind(this)}
deleteArgument={this.handleDeleteUpdateArgument.bind(this)}
upPosition={this.handleUpUpdateArgument.bind(this)}
downPosition={this.handleDownUpdateArgument.bind(this)}
Responses={this.state.selectedUpdateResponses}
/>
<Header/>
<NewCommand input={this.state.output} Arguments={this.state.currentArgs}
addCommand={this.handleAddCommand.bind(this)}
addArgument={this.handleAddArgument} deleteArgument={this.handleDeleteArgument.bind(this)}
upPosition={this.handleUpArgument.bind(this)} downPosition={this.handleDownArgument.bind(this)}

/>
<Commands input={this.state.output}
SelectUpdateObject={this.changeUpdateObject.bind(this)}
SendResponses={this.SendResponses.bind(this)}/>

</div>
);
}
}

export default App;

最佳答案

因为您在 SendResponses 方法中重写了 setState 方法,而不是调用它。您基本上将对象分配给它,这肯定是您不打算做的事情。这就是为什么你的第二个电话没有达到预期的效果。变化:

this.setState=({
selectedUpdateResponses:Response,
UpdateArguments:Arguments
});

至:

this.setState({
selectedUpdateResponses:Response,
UpdateArguments:Arguments
});

关于javascript - 仅在第二次调用函数时 react this.setState 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45813600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com