hooks
hooks copied to clipboard
feat(useSelections): add clearAll result
🤔 This is a ...
- [x] New feature
- [ ] Bug fix
- [x] Site / documentation update
- [ ] Demo update
- [ ] TypeScript definition update
- [ ] Bundle size optimization
- [ ] Performance optimization
- [ ] Enhancement feature
- [ ] Internationalization
- [ ] Refactoring
- [ ] Code style optimization
- [x] Test Case
- [ ] Branch merge
- [ ] Other (about what?)
🔗 Related issue link
💡 Background and solution
If the incoming array parameters items
are changed, the selected value will never be cleared and the old value will be present in selected.
e.g.
const [list, setList] = useState([{ a: 1 }, { a: 2 }]);
const { selected, selectAll, unSelectAll, clearAll } = useSelections(list);
selectAll();
console.log(selected); // === [{ a: 1 }, { a: 2 }]
// ...
setList([{ a: 3 }, { a: 4 }]);
unSelectAll();
console.log(selected); // ❌ === [{ a: 1 }, { a: 2 }]
// ...
clearAll();
console.log(selected); // ✅ === []
📝 Changelog
Language | Changelog |
---|---|
🇺🇸 English | feat(useSelections): add clearAll result |
🇨🇳 Chinese | feat(useSelections): 支持 clearAll 清除所有已选值 |
☑️ Self Check before Merge
⚠️ Please check all items below before review. ⚠️
- [x] Doc is updated/provided or not needed
- [x] Demo is updated/provided or not needed
- [x] TypeScript definition is updated/provided or not needed
- [x] Changelog is provided or not needed
这是 useSelections 不支持"非基础类型的项"(例如:对象作为数组项)导致的,示例代码中的用法:[{ a: 1 }, { a: 2 }]
目前是不支持这样的使用的。
目前这个 PR 只是绕过了根本问题而做的临时解决,不太推荐。
可以让 useSelections 支持复杂类型的项,可以来个新 PR,这样这个 PR 对应的问题自然就没了。
@liuyib 这个问题应该和对象数组没关系,改成int数组也是成立的。 究其原因是items入参数据被改变。state 中始终有上一次的数据,无法被清除。 而源数组被改变的场景也很普遍,如翻页或tabs list。 我不认为应该 close。
@liuyib 这个问题应该和对象数组没关系,改成int数组也是成立的。 究其原因是items入参数据被改变。state 中始终有上一次的数据,无法被清除。 而源数组被改变的场景也很普遍,如翻页或tabs list。 我不认为应该 close。
确实如你所说,之前看的不够仔细
我更新下 unSelectAll, clearAll 的文档解释,目前看不出区别
@liuyib 其实 items 是支持 object[] 的,函数内部实现用 set。判断 isSelected 等也有没问题。 保证 object 不变即可。 const list = [{a: 1}, {b: 2}]; const set = new Set();
set.add(list[0]); set.has(list[0]); // true
@liuyib 其实 items 是支持 object[] 的,函数内部实现用 set。判断 isSelected 等也有没问题。 保证 object 不变即可。 const list = [{a: 1}, {b: 2}]; const set = new Set();
set.add(list[0]); set.has(list[0]); // true
单从引用地址方面来看,当然是支持的,但实际是不方便用同一个引用去检索的,见:https://github.com/alibaba/hooks/issues/1105, 中的置顶 demo,这是比较真实的使用场景(当然也有纯逻辑不需要 UI 的场景可能比较适合用引用索引)。
实际使用时,如果数据项是对象(例如 [{ id: 1 }, { id: 2 }, { id: 3 }]
)通过 id 之类的字段去索引数据的位置才是通用的方便的做法,使用对象的引用地址去索引理论上可以,实际业务中(偏底层代码除外)用处比较少
@liuyib 请问 这个要怎么合?
等有权限的大佬合并,大佬在忙,等吧