我不知道我的folderPathName状态没有更新。 每次单击按钮时,我会将一个数组连接到folderPathName上。来自文件夹的数据是正确的,但是每次我回到事件folderPathNames时,它都是null,并且没有从上次单击按钮时的先前状态进行更新。
class Dashboard extends Component {
constructor(props) {
super(props)
this.state = {
folderPathNames: []
};
}
handleFolderClick = (event,id) => {
const { folderPathNames } = this.state;
const folderId = event.currentTarget.dataset["folder"]
const folder = this.state.model.folderDictionary[folderId]
let folderPath = [];
folderPath = folderPathNames.concat([folder])
this.setState({ ...this.state, folderPathNames: folderPath })
if (!!folder) {
this.props.listFolders(folder, this.state.model)
this.props.listDocument(+folderId, this.state.model)
}
this.setState({ enableCheckBox: false })
}
答案 0 :(得分:0)
该行可能以this.state中的现有键值结尾,这将导致folderPathNames
的值重复,我想Javascript会选择该键的第一个值?
this.setState({ folderPathNames: folderPath })
可能将其更改为:
this.setState({ ...this.state, folderPathNames: folderPath })
除非出于某种原因,您需要将现有状态传递回setState调用?