RoyalIcing / react-organism

Dead simple React state management to bring pure components alive
https://react-organism.now.sh/
MIT License
223 stars 3 forks source link

Add memoization #5

Open RoyalIcing opened 7 years ago

RoyalIcing commented 7 years ago
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 commented 7 years ago

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