gpt4 book ai didi

javascript - 从 React fetch 调用组件方法

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

我有一个名为 SeanceManager组件。在该组件中,我有一个方法,它使用fetch从后端请求数据。

class SeanceManager extends React.Component {

constructor(...args) {
super(...args);

this.state = {
addSeanceModalShow: false,
seances: [],
};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleDeleteUser = this.handleDeleteUser.bind(this);
this.fetchSeance = this.fetchSeance.bind(this);
}

componentDidMount() {
this.fetchSeance()
}



fetchSeance = () => {
fetch("/seances")
.then(res => res.json())
.then(
(result) => {
this.setState({
addSeanceModalShow: false,
seances: result
});
console.log("Fetchime: "+this.state.seances);
},

(error) => {
this.setState({
addSeanceModalShow: true,
error
});
}
)
console.log("l6pp: "+this.state.seances);
}

handleSubmit = (event, devices, interval, startDate, endDate) => {
event.preventDefault();
if (devices && interval && startDate && endDate) {
var data = { devices: devices, interval: interval, startDate: startDate, endDate: endDate };
fetch('/create', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
}).then(function (response) {
console.log(response)
//TODO: Should handle here somehow ?
// return response;
}).then(function (data) {
// console.log(data);
//------------------------Method crashing here-----------
this.fetchSeance();
//---------------------------------------------------
});

// this.fetchSeance();
}
};

handleDeleteUser = id => {
fetch('/delete/'+id, {
method: 'delete',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(id)
}).then(function (response) {
console.log("Kustumise response");
console.log(response);
//TODO: Should handle here somehow ?
// return response;
}).then(function (data) {
// console.log(data);
});
this.fetchSeance();
this.setState({state: this.state});
};

render() {
let addSeanceModalClose = () => this.setState({ addSeanceModalShow: false });

return (

<MDBRow>
<MDBCol className="col d-flex justify-content-center">
<MDBCard className="card col-md-6">
<MDBCardBody>
<div className="add-button-parent">
<MDBBtn className="add-button-child" color="cyan"
onClick={() => this.setState({ addSeanceModalShow: true })}>
Lisa uus seanss
</MDBBtn>
</div>

<form>
<AddSeanceFormModal
show={this.state.addSeanceModalShow}
handleSubmit={this.handleSubmit}
onHide={addSeanceModalClose}
/>
{/*{this.addToSeanceList()}*/}
<div className="card scrollbar-ripe-malinka ">
<div className="row ">
<div className="container-fluid">
<div className="card-body">
<h4 id="section1"><strong>Aktiivsed seansid</strong></h4>
<Seances
handleDeleteUser={this.handleDeleteUser}
seances={this.state.seances} />

</div>
</div>
</div>
</div>
</form>
</MDBCardBody>
</MDBCard>
</MDBCol>
</MDBRow>
);
}

};

export default SeanceManager;

但是当在 fetch 方法内部调用方法 this.fetchSeance() 时,我得到:

Unhandled Rejection (TypeError): Cannot read property 'fetchSeance' of undefined

如何从 fetch 中调用该方法?

最佳答案

您使用过

}).then(function (data) {
// console.log(data);
//------------------------Method crashing here-----------
this.fetchSeance();
//---------------------------------------------------
});

当您应该使用箭头函数来保持 this 与外部作用域相同时。

}).then((data) => {
// console.log(data);
//------------------------Method crashing here-----------
this.fetchSeance();
//---------------------------------------------------
});

再次更改:

}).then(function (data) {

至:

}).then((data) => {

关于javascript - 从 React fetch 调用组件方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56150456/

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