fix: Fixed single-select Cascader, under React18, the expansion behav… #2435
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
…ior is abnormal when loading data asynchronously
中文模板 / Chinese Template
What kind of change does this PR introduce? (check at least one)
PR description
Fixes #
单选的 Cascader 在 React 18 中会出现此问题。
Cascader 的展开项和 state 中 activeKeys 相关,点击会触发 activeKeys 变化。
在点击项目时,会调用 foundation 中的 handleItemClick -> handleSingleSelect->正确更新 activeKeys,由于需要异步加载数据,因此调用 notifyIfLoadData ->用户设置的 loadData 。
loadData 返回一个 promise,执行会触发treeData 更新, 并 resolve。
在 react 18 之前,treeData 的更新比 then 函数中的 loading 和 loadedKeys 更新快,因此updateSelectedKey函数中,可以借助没更新的 loading 和 loadedKeys 决定不改变 activeKeys。
在 react 18 之后,猜测对 promise 和 then 中的状态更新逻辑发生变化,导致updateSelectedKey执行时,loading 和 loadedKeys 已经变化,因此执行结果不符合预期。activeKeys 被更改。
修改方式为 在 loadData 的 then 函数中,借助 setTimeout 保证执行顺序。保证updateSelectedKey执行时,loading 和 loadedKeys 未变化。
Changelog
🇨🇳 Chinese
🇺🇸 English
Checklist
Other
Additional information