Skip to content

Instantly share code, notes, and snippets.

View hongkheng's full-sized avatar
🐢
doing hobby stuff

HongKheng Yap hongkheng

🐢
doing hobby stuff
View GitHub Profile
import { useState, useEffect } from 'react'
export const useLocalStorage = (key, value = undefined) => {
const [storeValue, setStoreValue] = useState(() => {
// get value if exists in localStorage
const item = window.localStorage.getItem(key)
return item
})
useEffect(() => {
import { useState, useEffect } from 'react'
export const useLocalStorage = (key, value = undefined) => {
const [storeValue, setStoreValue] = useState(() => {
// get value if exists in localStorage
const item = window.localStorage.getItem(key)
return item
})
useEffect(() => {
@hongkheng
hongkheng / jsdom-error.md
Created April 30, 2020 17:31
Jest/jsdom navigation errors

To resolve errors from jest test such as:

Error: Not implemented: navigation (except hash changes)
 at module.exports (/Users/pyhk329d/workspace/sppower-projects/lighthouse-web/node_modules/jsdom/lib/jsdom/browser/not-implemented.js:9:17)
at navigateFetch (/Users/pyhk329d/workspace/sppower-projects/lighthouse-web/node_modules/jsdom/lib/jsdom/living/window/navigation.js:77:3)
 at exports.navigate (/Users/pyhk329d/workspace/sppower-projects/lighthouse-web/node_modules/jsdom/lib/jsdom/living/window/navigation.js:55:3)
 at Timeout._onTimeout (/Users/pyhk329d/workspace/sppower-projects/lighthouse-web/node_modules/jsdom/lib/jsdom/living/nodes/HTMLHyperlinkElementUtils-impl.js:81:7)
at listOnTimeout (internal/timers.js:549:17)
 at processTimers (internal/timers.js:492:7) undefined
@hongkheng
hongkheng / react-hoc.js
Last active September 20, 2020 05:14
React Higher Order Components
// HOC FactoryFactory method
function HOCFactoryFactory (...params) {
console.log('params', params)
return function HOCFactory (WrappedComponent) {
return class withHOCFactory extends React.Component {
render () {
return <WrappedComponent {...this.props} />
}
}
}
@hongkheng
hongkheng / tobindornottobind.js
Last active December 7, 2018 06:57
Regarding `bind` in constructor for class methods
// Bind class methods that would be call externally by when executing `new Foo()`
// context to `this` for the current class context will be lost when assigning the method to another object
// e.g
// Binding a method as an instance property
class Bar {
constructor() {
//this.bar = this.bar.bind(this); // <-- this line is required when obj reference is being passed around such that the context of `this` is lost
this.counter = 0
}
@hongkheng
hongkheng / jest-mocks-examples.js
Last active December 7, 2018 05:45
Jest mocks
// Show how to setup and create a simple Jest manual mocks
// Beginning of the example... custom.test.js
// Assume your Custom ES module and import it
import Custom from './Custom';
// Has to be after it is imported, use the file path where the module is located
jest.mock('./Custom', () => {
const mockMethod1= jest.fn()
@hongkheng
hongkheng / notes.go
Last active June 20, 2019 08:30
Ultimate Go notes
// Variables consistency
var defaultZero int // always contain 0
var defaultEmpty string // always be ""
// https://play.golang.org/p/xD_6ghgB7wm
// struct types
/**
Compiler does not do implicit type conversion for named type.
For literal type, compiler does implicit type conversion, for type = literal type
@hongkheng
hongkheng / ReactComponent.js
Last active May 15, 2018 06:54
Ways to define a React component
/*
This gist explains the several ways to define a React component
*/
// Functional components that has no life cycle methods tied to itself
// Using the function
function SimpleFunction(props) {
// passing in the props that is given by React by default
return (
<div>