简体   繁体   English

用现有的 ReactJs 更新/重置 state

[英]Update/reset state with existing one - ReactJs

I have quite an head scratcher here.我在这里有一个令人头疼的问题。 I have some check boxes.我有一些复选框。 After check box is selected you are abble to download a document based on the selected checkbox.选中复选框后,您可以根据选中的复选框下载文档。 After my download is completed i disable my checkbox, but still it is marked as checked.下载完成后,我禁用了我的复选框,但它仍然被标记为选中。 UI wise i inform the user that this section is downloaded. UI 方面,我通知用户此部分已下载。 But if a user selects another checkbox from the same page the previous one is enabled again ( so it is not disabled anymore ).但是,如果用户从同一页面选择另一个复选框,则会再次启用前一个复选框(因此不再禁用)。 My question is how can i update or reset my state in order to have all check boxes disabled and download only the documents that are selected at the moment and not the previous ones.我的问题是如何更新或重置我的 state 以禁用所有复选框并仅下载当前选择的文档而不下载以前的文档。

   constructor(props) {
    super(props);
    this.state = { docList: {} }

after that this is my function to handle the checkboxes:之后这是我的 function 来处理复选框:

    handleCheckboxClick = (e) => {
let parsedVal = JSON.parse(e.target.value);
let newDocList = { ...this.state.docList };
if (e.target.checked) {
    newDocList[parsedVal.documentId] = parsedVal.documentNumber;
} else {
    delete newDocList[parsedVal.documentId];
}
this.setState({
    docList: newDocList,
}, () => {
    console.log(this.state.docList)
});

after that is my axios.post method:之后是我的axios.post方法:

    axios.post(SERVER_URL + '/api/user/bills/zip', postZipData, axiosZipConfig)

        .then((response) => {
            this.setState({
                downloadedIDS: response.data.downloadedIds,
                docList: this.state.docList
            })    e.preventDefault();

and the render:和渲染:

const { downloadedIDS, docList } = this.state;

render: rowData =>
              <Checkbox
              disabled={downloadedIDS && downloadedIDS.indexOf(rowData.documentId) >= 0 ? true : false}
              color='default'
              value={JSON.stringify({ documentId: rowData.documentId, documentNumber: rowData.documentNumber })}
              onClick={this.handleCheckboxClick} />

The result is this:结果是这样的: 在此处输入图像描述

Any advice is welcome how should i treat my code.欢迎任何建议我应该如何处理我的代码。

In this section...在这个部分...

.then((response) => {
            this.setState({
                downloadedIDS: response.data.downloadedIds,
                docList: this.state.docList
            })    e.preventDefault();

It seems you're overwriting downloadedIds with the IDs from the response that's just returned.您似乎正在使用刚刚返回的响应中的 ID 覆盖downloadedIds的 ID。 If I understand correctly, it seems like you should append the IDs from the response to the existing state of downloadedIds .如果我理解正确,您似乎应该 append 来自对现有downloadedIds的响应的 ID 。

Assuming downloadedIds is an array here is how you can achieve this using the array spread operator .假设downloadedIds的Ids 是一个数组,您可以使用数组扩展运算符来实现这一点。

this.setState({
    downloadedIds: [
        ...this.state.downloadedIds,
        ...response.data.downloadedIds
    ]
})

There is no point in setting docList state to equal this.state.docList that will just set the state to equal the same thing, so it's not required.将 docList state 设置为等于this.state.docList是没有意义的,它只会将 state 设置为等于相同的值,所以它不是必需的。

Hope this helps, let me know if I can go into any more detail.希望这会有所帮助,让我知道我是否可以更详细地了解 go。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM