如何使用会话存储来保存所选的过滤值?即使用户刷新页面,我也想存储所选的过滤器值
这是我到目前为止设置的过滤器。但是我很难找到一种存储所选值的方法。我是否应该使用本地存储或会话存储来在这里尝试实现的目标?
const [filter, setFilter]= React.useState<AssignedFilterTypes>(AssignedFilterTypes.All);
const res = content.filter(c => c.percentage !== 100).sort((a, b) => compare(a, b, sort));
const storedFilter = sessionStorage.setItem('storedFilter',filter)
if(statistics && filter !== AssignedFilterTypes.All) {
return res.filter((c) => statistics[c.id].num_users);
}
return res;
}, [sort, showContent, filter, courses, programs, packages]);
Here's the filter that I have set up so far. But I am having trouble finding a way to store the selected values. Should I be using local storage or session storage for what I am trying to achieve here?
const [filter, setFilter]= React.useState<AssignedFilterTypes>(AssignedFilterTypes.All);
const res = content.filter(c => c.percentage !== 100).sort((a, b) => compare(a, b, sort));
const storedFilter = sessionStorage.setItem('storedFilter',filter)
if(statistics && filter !== AssignedFilterTypes.All) {
return res.filter((c) => statistics[c.id].num_users);
}
return res;
}, [sort, showContent, filter, courses, programs, packages]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关闭选项卡时,会话存储将清除。但是,当页面重新加载时,它持续存在。如果您不想在关闭浏览器时丢失数据,则需要本地存储。请注意,如果您在私人窗口中,则将其关闭后将清除。
Session storage gets cleared when the tab is closed. It persists when the page reloads, though. If you don't want to lose the data when the browser is closed, you need local storage. Note that if you are in a private window, it will clear after you close it.