javascript - REACTJS 中状态变化的错误/问题
问题描述
我想通过 Axios 将 id book 和 returnDate 发送到后面。但是,对于 id 来说没问题,但对于 returnDate,值保持“未定义”。我可以在我的代码中添加/更改什么,以便使用代码末尾输入中输入的日期更改 returnDate?所以在 handleSubmit 函数中使用它。谢谢你。
import { MDBBtn, MDBTableBody, MDBTableHead, MDBTable, MDBModal, MDBModalHeader, MDBModalBody, MDBModalFooter} from "mdbreact";
import axios from 'axios';
class MyLends extends React.Component{
constructor(props) {
super(props);
this.state = {
lends: null,
returnDate: null,
modal: false,
id: '',
error:'',
}
this.handleSubmit = this.handleSubmit.bind(this);
}
toggle = () => {
this.setState({
modal: !this.state.modal
});
}
change = e => {
this.setState({
[e.target.id]: e.target.value,
});
}
handleSubmit = (id, returnDate) => e => {
console.log(id, returnDate);
const { lends } = this.state;
let newBooks = lends;
let formData = new FormData();
formData.append("id",id);
formData.append("returnDate",returnDate);
const url = "http://localhost:8888/liste_prets/rendu_pret.php"
axios.post(url, formData)
.then(function(response){
const tmp = lends;
newBooks = tmp.filter(livre => livre.id !== id);
alert('Succès : Livre à nouveau marqué comme disponible');
})
.catch((error)=>{
console.log(error)
if (error.response.status === 403){
console.log(error);
alert('Echec du rendu');
}
});
setTimeout(() => {
this.setState({
lends: newBooks
})
}, 500);
setTimeout(() => {
this.setState({
error: '',
});
}, 2000);
e.preventDefault();
}
async componentDidMount() {
const url = "http://localhost:8888/liste_prets/liste.php";
const response = await fetch(url);
const data = await response.json();
this.setState({lends: data.results.livres, loading: false})
console.log(data.results.livres);
}
render() {
return (
<div>
<MDBTable hover>
<MDBTableHead>
<tr>
<th>Titre</th>
<th>Auteur</th>
<th>Emprunteur</th>
<th>Date d'emprunt</th>
<th></th>
</tr>
</MDBTableHead>
<MDBTableBody>
{
this.state.lends.map(livre => (
<tr key={livre.id} onChange={this.Change} >
<td>{livre.title}</td>
<td>{livre.author}</td>
<td>{livre.borrower}</td>
<td>{livre.borrowDate}</td>
<td><MDBBtn onClick={this.toggle} color="dark">Rendu</MDBBtn>
<MDBModal isOpen={this.state.modal} toggle={this.toggle}>
<MDBModalHeader toggle={this.toggle}>Date de rendu</MDBModalHeader>
<MDBModalBody>
<input
type="date"
id="returnDate"
placeholder="jj/mm/aa"
className="form-control"
onChange={this.Change}
/>
</MDBModalBody>
<MDBModalFooter>
<MDBBtn color="secondary" onClick={this.toggle}>Annuler</MDBBtn>
<MDBBtn color="primary" onClick={this.handleSubmit(livre.id, this.returnDate)}>Enregistrer</MDBBtn>
</MDBModalFooter>
</MDBModal>
</td>
</tr>
))
}
</MDBTableBody>
</MDBTable>
</div>
);
}
}
export default MyLends;
解决方案
你可以只 setState onthis.change
因为你已经绑定了它输入的 onChange 事件。并且不要忘记 javascript区分大小写
change = e => {
this.setState({
returnDate: e.target.value,
});
}
在您的输入中,您可能希望将 onChange 降低到onChange={this.change}
额外的;
您应该访问 returnDate 为;this.handleSubmit(livre.id, this.state.returnDate)
推荐阅读
- ios - UITabBar 项目在 iOS 12.1 上跳转后退导航
- python - 我想比较列表中索引的值,但一个索引不存在,我该如何处理该错误
- r - 如何使用knitr在代码块内的子图中插入分页符
- rest - 访问 Magento 2 Rest API 的 HTTP 401 基本身份验证错误
- java - I-net Clear Report 缓存旧数据
- powerbi - 将前导 0(零)添加到 Power BI 中的月份数
- qemu - 在 Windows 10(64 位)下为 Windows 10(64 位)编译 QEMU
- javascript - 具有多个字符串输入的切换案例
- python - 使用两个循环将结果写入 csv 文件
- javascript - 使用 Javascript 为动态生成的文本区域实现漂亮的编辑器