Skip to content

Instantly share code, notes, and snippets.

const immutableMap = Immutable.fromJS({ a: 1, b: 2 })
const { a, b } = immutableMap
console.log(a) // undefined
console.log(b) // undefined
const immu = obj => deepFreeze(obj)
const createNewState = (currentState, updater) => {
const draftState = deepUnfreeze(currentState)
updater(draftState)
return immu(draftState)
}
export { immu, createNewState }
const initialState = {
todos: {
'a': { id: 'a', description: '...', isComplete: false },
'b': { id" 'b', description: '...', isComplete: false }
}
}
const reducer = (state, action) => ({
...state,
todos: {
import { createAction, createReducer } from 'redux-toolkit'
import { initialState } from 'the-gist-above'
// Another utility from the toolkit - createAction
const toggleTodo = createAction('increment')
const reducer = createReducer(initialState, {
[toggleTodo]: (state, action) => {
state.todos[action.todoId].isComplete = action.payload
}
useEffect(() => {
const fetchShops = async () => {
try {
const shops = await axios.get(process.env.GATSBY_COFFEE_SHOPS_URL)
setShops(shops.data.Items)
} catch (err) {
setShopsError(true)
} finally {
setShopsLoading(false)
}
// gatsby-node.js file
const axios = require('axios')
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`, // this dotenv config gives access to process.env object
})
const getCoffeeShops = async () => {
const shops = await axios.get(process.env.GATSBY_COFFEE_SHOPS_URL)
return shops.data.Items
app.post('/getData', (req, res) => {
res.send(generateData(User));
});
app.post('/getFlyWeightData', (req, res) => {
res.send({ data: generateData(FlyWeightUser), EXTRA_STATIC_DATA })
});
chrome.webRequest.onBeforeRequest.addListener(
request => {
const url = new URL (request.url)
url.searchParams.set('tag', 'MY_REFERRAL_TAG')
return {
redirectUrl: url.toString()
}
},
{ urls: ['https://amazon.co.uk/*] }, ['blocking']
)
import { useState } from 'react';
const useFetch = (fetchRequest, fetchName = 'is') => {
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
const [isSuccess, setIsSuccess] = useState(false);
const [fetchResponse, setFetchResponse] = useState(null);
const resetFetchState = () => {
setIsLoading(false);
import { createContext, useReducer, useContext } from 'react';
import { bindActionCreators } from '~/context/utils';
const initialState = {
modalOpen: null,
};
const actionTypes = {
OPEN_MODAL: 'OPEN_MODAL',
CLOSE_MODALS: 'CLOSE_MODALS',