A React higher order component to prefetch component data.
npm:
npm install react-wildcat-prefetch
@prefetch({string}|{function} action, [{Object} options])
// Prefetch data from a fully qualified URL
@prefetch("https://example.com/data.json")
// Or pass in a function
function getData() {
return Promise.resolve(/* some data */);
}
@prefetch(getData)
// By default the key name is `asyncData`, but you
// can set your own key name in the options object
@prefetch("https://example.com/data.json", {key: "newKeyName"})
Full example can be found in the examples folder
import prefetch from "react-wildcat-prefetch";
@prefetch("https://example.com/data.json", {key: "asyncData"})
class PrefetchExample extends React.Component {
static propTypes = {
asyncData: PropTypes.object.isRequired
};
static defaultProps = {
asyncData: {
data: []
}
};
render() {
const {asyncData: {data}} = this.props;
console.log(data);
}
}