react-organism icon indicating copy to clipboard operation
react-organism copied to clipboard

Add memoization

Open RoyalIcing opened this issue 8 years ago • 1 comments
trafficstars

import makeMemory from 'react-organism/makeMemory'

const remember = makeMemory(3) // Capacity of 3 per action

export const initial = () => ({ posts: null })

// Remember caches given the same props
export const loadPosts = remember(
  ({ postID }) => fetch(`/posts/${postID}`).then(res => res.json()).then(posts => ({ posts }))
)

export const load = (props, prevProps) => {
  //actions.loadPosts() or
  return Promise.all([
    loadPosts(props) // Will reuse previously cached if props were the same
  ])
}

RoyalIcing avatar Jun 26 '17 05:06 RoyalIcing

Maybe use https://github.com/reactjs/reselect

RoyalIcing avatar Aug 16 '17 05:08 RoyalIcing