Skip to content

Instantly share code, notes, and snippets.

@brigand
Last active March 14, 2016 00:05
Show Gist options
  • Save brigand/16074e8a5cc208c05447 to your computer and use it in GitHub Desktop.
Save brigand/16074e8a5cc208c05447 to your computer and use it in GitHub Desktop.
react-obcache - api redesign
import ObCache from 'obcache';
var cache = new ObCache();
cache.register('userInfo', ([id]) => functionThatReturnsAPromise(id));
export default cache;
import React from 'react';
import cache from './cache';
import {providesCacheObserver} from 'react-obcache';
export class UserInfo extends React.Component {
renderWithCache = (getFromCache) => {
var user = getFromCache('userInfo', this.props.userId);
if (user.error) return <div>404</div>;
if (!user.data) return <div>Loading</div>;
return (
<div>
{user.data.firstName} {user.lastName}
</div>
);
}
render() {
this.props.renderAndTrack(this.renderWithCache);
}
}
export default providesCacheObserver({cache}, UserInfo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment