Skip to content

Instantly share code, notes, and snippets.

View geraldyeo's full-sized avatar
🎯
Focusing

Gerald Yeo geraldyeo

🎯
Focusing
View GitHub Profile
@nguyenit67
nguyenit67 / async-await-without-trycatch-hell.js
Created August 14, 2021 04:08
Async Await try-catch hell
// #1 catch in promise-based
await step1().catch(fun);
async function gettingBetter() {
const a = await step1().catch(err => handle(err));
const b = await step2().catch(err => handle(err));
const c = await step3().catch(err => handle(err));
}
// #2 create a function handle try catch => [data, error]
async function awesome() {
@Tymek
Tymek / useNavigationLock.ts
Last active May 25, 2023 22:38
Next.js block navigation on unsaved changes
import { useEffect, useState } from 'react';
import { useRouter } from 'next/router';
/** Ask for confirmation before changing page or leaving site.
*
* @see https://git.io/JOskG
*/
const useNavigationLock = (
isEnabled = true,
warningText = 'You have unsaved changes – are you sure you wish to leave this page?',
@iest
iest / readme.md
Last active October 19, 2022 08:46
Moving from lodash/fp to ramda

Moving from lodash/fp to ramda

How

Good news is we're only using lodash/fp, which makes it easier to match function signatures.

  1. Find most-used lodash methods, we'll convert these first maybe?
  2. Go through each lodash method, find the ramda equivalent where possible
  3. Write a codemod to rewrite those usages
  4. Who the fuck thought function aliases were a good idea
@culttm
culttm / axios.refresh_token.js
Created October 5, 2017 18:46
axios interceptors for refresh token
axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
const originalRequest = error.config;
if (error.response.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;
@javisperez
javisperez / interceptors.js
Last active December 4, 2022 16:47
Axios interceptor for cache with js-cache
// Usually I use this in my app's config file, in case I need to disable all cache from the app
// Cache is from `js-cache`, something like `import Cache from 'js-cache';`
const cacheable = true,
cache = new Cache();
// On request, return the cached version, if any
axios.interceptors.request.use(request => {
// Only cache GET requests
if (request.method === 'get' && cacheable) {
@jschloer
jschloer / rrv4index.ios.js
Last active January 4, 2017 04:08
React Router V4 for React Native
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
@gcanti
gcanti / HOC.js
Created September 14, 2016 09:24
/* @flow */
import React from 'react'
import ReactDOM from 'react-dom'
type FunctionComponent<A> = (props: A) => ?React$Element<any>;
type ClassComponent<D, A, S> = Class<React$Component<D, A, S>>;
type Component<A> = FunctionComponent<A> | ClassComponent<any, A, any>;
@acdlite
acdlite / app.js
Last active January 20, 2023 08:23
Quick and dirty code splitting with React Router v4
// getComponent is a function that returns a promise for a component
// It will not be called until the first mount
function asyncComponent(getComponent) {
return class AsyncComponent extends React.Component {
static Component = null;
state = { Component: AsyncComponent.Component };
componentWillMount() {
if (!this.state.Component) {
getComponent().then(Component => {

Transduce


What is transduce? What is it for? This document is intended to help people (such as myself) who would be looking through the ramda docs, find transduce and have no idea if it would be a good fit for my current problem.

@MoOx
MoOx / package.json.js
Last active June 26, 2022 22:18
Boost your Webpack performance with DLLPlugin (will bundle as dll all your "dependencies", see comment in package.json)
{
"private": true,
// ...
"#dependencies": "dependencies are the one shipped to the client",
"dependencies": {
"babel-polyfill": "^6.7.4",
"react": "^15.0.0",
// ...
"whatwg-fetch": "^0.11.1"
},