Created
July 11, 2017 15:21
-
-
Save c0d0g3n/69e9902804bede030422fcad8541d8bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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