Skip to content

Instantly share code, notes, and snippets.

@guigonc
guigonc / polyfill.js
Created August 17, 2021 19:00
Media query polyfill for Safari prior to 14. The API Support is described here https://caniuse.com/mdn-api_mediaquerylist
(function mediaQueryPolyfill() {
const mediaQueryAll = window.matchMedia('all');
if (typeof mediaQueryAll.addEventListener == 'function') return
const defaultMatchMedia = window.matchMedia;
window.matchMedia = function (query) {
const mediaQuery = defaultMatchMedia(query);
@guigonc
guigonc / accountActions.js
Created October 20, 2017 16:28
Code snippet #10 for Medium post "Moving API requests from a React component to a high-order component"
const fetchAccountsIfNeeded = () => (dispatch, getState) => {
if (shouldFetchAccounts(getState()))
return dispatch(fetchAccounts())
}
const shouldFetchAccounts = ({ accounts }) => !accounts.fetching && !accounts.upToDate
const fetchAccounts = () => dispatch => {
dispatch(requestAccounts())
return axios.get(`${apiHost}${accountsPath}`)
@guigonc
guigonc / accountList.jsx
Created October 2, 2017 19:15
Code snippet #9 for Medium post "Moving API requests from a React component to a high-order component"
const mapStateToProps = (state) => ({
accounts: state.accounts.items
})
const mapDispatchToProps = (dispatch) => ({
fetchAccounts: () => dispatch(fetchAccountsIfNeeded())
})
export default compose(
connect(mapStateToProps, mapDispatchToProps),
@guigonc
guigonc / compose.js
Created October 2, 2017 19:14
Code snippet #8 for Medium post "Moving API requests from a React component to a high-order component"
const compose = (f, g) => (x) => f(g(x))
@guigonc
guigonc / accountList.jsx
Last active October 2, 2017 19:12
Code snippet #7 for Medium post "Moving API requests from a React component to a high-order component"
const mapStateToProps = (state) => ({
accounts: state.accounts.items
})
const mapDispatchToProps = (dispatch) => ({
fetchAccounts: () => dispatch(fetchAccountsIfNeeded())
})
export default connect(
mapStateToProps,
@guigonc
guigonc / callOnMount.jsx
Created October 2, 2017 19:09
Code snippet #6 for Medium post "Moving API requests from a React component to a high-order component"
const callOnMount = callback => WrappedComponent => class extends Component {
componentDidMount() {
callback(this.props)
}
render() {
return (<WrappedComponent {...this.props} />)
}
}
@guigonc
guigonc / enhancedComponent.jsx
Last active October 2, 2017 19:10
Code snippet #5 for Medium post "Moving API requests from a React component to a high-order component"
const BareComponent = () => <div>Hello</div>
export default enhancer(BareComponent)
@guigonc
guigonc / accountList.jsx
Last active October 2, 2017 18:57
Code snippet #4 for Medium post "Moving API requests from a React component to a high-order component"
class AccountListContainer extends Component {
componentDidMount() {
this.props.fetchAccounts()
}
render() {
return (<AccountList {...this.props} />)
}
}
@guigonc
guigonc / accountList.jsx
Last active October 2, 2017 18:58
Code snippet #3 for Medium post "Moving API requests from a React component to a high-order component"
const AccountList = ({ accounts }) => (<ul>
{
accounts.map(account =>
<li key={account.id}>{account.name}</li>
)
}
</ul>)
@guigonc
guigonc / accountList.jsx
Last active October 2, 2017 18:58
Code snippet #2 for Medium post "Moving API requests from a React component to a high-order component"
const mapStateToProps = state => ({
accounts: state.accounts.items
})
const mapDispatchToProps = dispatch => ({
fetchAccounts: () => dispatch(fetchAccountsIfNeeded())
})
export default connect(mapStateToProps, mapDispatchToProps)(AccountList)