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
import type { History, Location } from 'history'; | |
type ReadonlyHistory<HistoryLocationState> = Omit< | |
Readonly<History<HistoryLocationState>>, | |
'location' | |
> & { | |
readonly location: Readonly<Location<HistoryLocationState>>; | |
}; | |
export default ReadonlyHistory; |
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
// Manage a capsule with the useCapsule hook. | |
import { useCapsule } from 'react-capsule'; | |
import myCapsule from '...'; | |
export default function MyComponent() { | |
const [value, setValue] = useCapsule(myCapsule); // 💊 | |
// Change my capsule's value on click. | |
const handleClick = React.useCallback(() => { | |
setValue('your value'); |
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
// Create a capsule by giving it an initial value. | |
import Capsule from 'react-capsule'; | |
export default new Capsule('my initial value'); |
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
import { addCallback, setGlobal } from 'reactn'; | |
// Every time the global state changes, this function will execute. | |
function prevent1(newGlobalState) { | |
alert(`The new value is ${newGlobalState.value}!`); | |
// If the global state was changed to 1, change it to 2. | |
if (newGlobalState.value === 1) { | |
return { value: 2 }; | |
} |
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 addTo1 = addSub(1); // nums = [1] | |
+addTo1(2); // 3 nums = [ 1, 2 ] | |
+addTo1(2); // 1 nums = [ 1, 2, 2 ] |
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
// Given an array of numbers, if the index is even, add. | |
// If the index is odd, subtract. | |
const addSubtractReducer = (total, current, index) => | |
(index % 2) === 0 ? | |
total + current : | |
total - current; | |
const addSubtract = x => { | |
const nums = [ ]; |
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 myStrObject = { | |
toString: function() { | |
return 'Str'; | |
} | |
}; | |
console.log('My object is ' + myStrObject); // 'My object is Str' | |
console.log(myStrObject + 297); // 'Str297' | |
const myNumObject = { | |
valueOf: function() { |
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 x = addSubtract(1)(2)(3); // function | |
+x; // type cast to 0 | |
+x(4); // type cast to 4 |
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
// This cannot be true with the following statement. | |
addSubtract(1)(2)(3) === 0; | |
// This cannot be true with the preceding statement. | |
addSubtract(1)(2)(3)(4)(5)(6) === 5; | |
// This can be true: | |
addSubtract(1)(2)(3) + addSubtract(1)(2)(3)(4)(5)(6) === 5; | |
// These can be true too: |
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
addSubtract(1)(2)(3); // 1 + 2 - 3 = 0 | |
addSubtract(1)(2)(3)(4)(5)(6); // 1 + 2 - 3 + 4 - 5 + 6 = 5 |
NewerOlder