This document details some tips and tricks for creating redux containers. Specifically, this document is looking at the mapDispatchToProps
argument of the connect
function from [react-redux][react-redux]. There are many ways to write the same thing in redux. This gist covers the various forms that mapDispatchToProps
can take.
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
const monthsLater = (date, months) => | |
[ | |
(dateArg) => new Date(dateArg), | |
(dateObj) => dateObj.setMonth(dateObj.getMonth() + months), | |
(timestamp) => new Date(timestamp), | |
String | |
].reduce( | |
(value, fn) => fn(value), | |
date | |
); |
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
"use strict"; | |
// animation utils | |
// =============== | |
const trackTime = id => { | |
const [entry] = performance.getEntriesByName(id); | |
if (!entry) { | |
performance.mark(id); |
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
/* | |
* Handling Errors using async/await | |
* Has to be used inside an async function | |
*/ | |
try { | |
const response = await axios.get('https://your.site/api/v1/bla/ble/bli'); | |
// Success 🎉 | |
console.log(response); | |
} catch (error) { | |
// Error 😨 |
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
function fibonacciProcessed(x) { | |
return new Promise(resolve => { | |
const calculation = function (resolveFn, n = 0, a = 0, b = 1) { | |
if (n > 1) { | |
setImmediate(() => { | |
calculation(resolveFn, n - 1, b, a + b); | |
}); | |
} | |
// else |
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
extension Int { | |
func formatUsingAbbrevation () -> String { | |
let numFormatter = NSNumberFormatter() | |
typealias Abbrevation = (threshold:Double, divisor:Double, suffix:String) | |
let abbreviations:[Abbrevation] = [(0, 1, ""), | |
(1000.0, 1000.0, "K"), | |
(100_000.0, 1_000_000.0, "M"), | |
(100_000_000.0, 1_000_000_000.0, "B")] |
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
/* | |
sqrt(8) | |
(0 + 8) / 2 = 4 | |
4 * 4 = 16 | |
(0 + 4) / 2 = 2 | |
2 * 2 = 4 | |
(2 + 4) / 2 = 3 | |
3 * 3 = 9 |
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
:root { | |
--ease-in-quad: cubic-bezier(.55, .085, .68, .53); | |
--ease-in-cubic: cubic-bezier(.550, .055, .675, .19); | |
--ease-in-quart: cubic-bezier(.895, .03, .685, .22); | |
--ease-in-quint: cubic-bezier(.755, .05, .855, .06); | |
--ease-in-expo: cubic-bezier(.95, .05, .795, .035); | |
--ease-in-circ: cubic-bezier(.6, .04, .98, .335); | |
--ease-out-quad: cubic-bezier(.25, .46, .45, .94); | |
--ease-out-cubic: cubic-bezier(.215, .61, .355, 1); |
A lot of people misunderstood Top-level await is a footgun, including me. I thought the primary danger was that people would be able to put things like AJAX requests in their top-level await
expressions, and that this was terrible because await
strongly encourages sequential operations even though a lot of the asynchronous activity we're talking about should actually happen concurrently.
But that's not the worst of it. Imperative module loading is intrinsically bad for app startup performance, in ways that are quite subtle.
Consider an app like this:
// main.js