Download reselect/docs/examples/weakMapMemoize/cacheSizeProblem.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 883 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/reselect/docs/examples/weakMapMemoize/cacheSizeProblem.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/reselect/docs/examples/weakMapMemoize/cacheSizeProblem.ts
-
curl -L -o cacheSizeProblem.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/reselect/docs/examples/weakMapMemoize/cacheSizeProblem.ts
883 Bytes
| import { createSelector } from 'reselect' | |
| export interface RootState { | |
| items: { id: number; category: string; name: string }[] | |
| } | |
| const state: RootState = { | |
| items: [ | |
| { id: 1, category: 'Electronics', name: 'Wireless Headphones' }, | |
| { id: 2, category: 'Books', name: 'The Great Gatsby' }, | |
| { id: 3, category: 'Home Appliances', name: 'Blender' }, | |
| { id: 4, category: 'Stationery', name: 'Sticky Notes' } | |
| ] | |
| } | |
| const selectItemsByCategory = createSelector( | |
| [ | |
| (state: RootState) => state.items, | |
| (state: RootState, category: string) => category | |
| ], | |
| (items, category) => items.filter(item => item.category === category) | |
| ) | |
| selectItemsByCategory(state, 'Electronics') // Selector runs | |
| selectItemsByCategory(state, 'Electronics') | |
| selectItemsByCategory(state, 'Stationery') // Selector runs | |
| selectItemsByCategory(state, 'Electronics') // Selector runs again! | |