Skip to content

Instantly share code, notes, and snippets.

@c0d0g3n
Created July 11, 2017 15:21
Show Gist options
  • Save c0d0g3n/69e9902804bede030422fcad8541d8bf to your computer and use it in GitHub Desktop.
Save c0d0g3n/69e9902804bede030422fcad8541d8bf to your computer and use it in GitHub Desktop.
// components/ComponentExample.js
import React from 'react'
import {connect} from 'react-redux'
import endpointExample from '../endpoints/endpointExample.js'
@connect((state, props) => {
return {
example: endpointExample(state, props)
}
})
export default class ComponentExample extends React.Component {
// ...
}
// helpers/createEndpoint.js
export default (options) => {
const endpoint = options
// storage for debouncing requests
let timeout
// endpoint
return (state, props) => {
// should request data?
if (endpoint.shouldRequest(endpoint, state, props)) {
if (timeout >= 0) {
clearTimeout(timeout)
}
timeout = endpoint.request(endpoint, state, props)
}
// return data
return endpoint.return(endpoint, state, props)
}
}
// endpoints/endpointExample.js
import createEndpoint from '../helpers/createEndpoint.js'
export default createEndpoint({
request: (endpoint, state, props) => {
// How should we hit this endpoint?
// return setTimeout to enable debouncing
// e.g.
},
return: (endpoint, state, props) => {
// What data should the component use for current render?
// Try returning an object reference to use react-redux optimalizations
},
shouldRequest: (endpoint, state, props) => {
// When should we send a request?
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment