tiny-atom icon indicating copy to clipboard operation
tiny-atom copied to clipboard

memoized selectors

Open pmualaba opened this issue 5 years ago • 1 comments

Hello, is this library able to do memoized selectors aka reselect? If yes how should it be used the "tiny-atom way" ?

pmualaba avatar Jul 12 '19 09:07 pmualaba

Create and export your selectors, e.g.:

import { createSelector } from 'reselect'

const getNotes = state => state.notes
const getTags = state => state.tags

export const notesWithTags = createSelector(
  [getNotes, getTags],
  (notes, tags) => notes.map(enhance(tags))
)

Then use them when mapping state in your components:

import { useAtom } from 'tiny-atom/react/hooks'
import { notesWithTags } from './selectors'

export function Notes () {
  let { notes } = useAtom(state => ({
    notes: notesWithTags(state)
  }))
  return <List notes={notes} />
}

Now component will only rerender if the selector returns different value. And the selector will only be recomputed if notes or tags in the state change.

KidkArolis avatar Jul 14 '19 22:07 KidkArolis