Skip to content

Instantly share code, notes, and snippets.

View wklsh's full-sized avatar

Shawn Lim Wee Kiang wklsh

View GitHub Profile
@wklsh
wklsh / dummy.md
Last active July 13, 2021 18:20
Async / await API example for dummies
@wklsh
wklsh / document.md
Last active October 9, 2020 17:25
React eventListener + state handling

React event listeners with stale closure workaround

Component.jsx

function Component(props) {
  const [isFocused, setIsFocused] = useState(false);
  const mainComponentRef = useRef();

  useEffect(() => {
 // detect if clicks are occurring outside of cart content area. If it is, hide module
@wklsh
wklsh / UIContext.md
Last active February 13, 2022 14:34
Redux-like React Context Example

Redux-like React Context example

Example Setup of a redux-like react context. For reference visit this guide, or this guide.

Store

import React, { createContext, useReducer } from "react";

export const UIContext = createContext();
@wklsh
wklsh / docker_wordpress.md
Created June 23, 2020 10:29 — forked from bradtraversy/docker_wordpress.md
Docker Compose FIle For Wordpress, MySQL & phpmyadmin

Wordpress & Docker

This file will setup Wordpress, MySQL & PHPMyAdmin with a single command. Add the code below to a file called "docker-compose.yaml" and run the command

$ docker-compose up -d

# To Tear Down
$ docker-compose down --volumes
@wklsh
wklsh / style.scss
Last active June 17, 2020 12:24
Reponsive square CSS elements – Revised
.squareWrapper {
width: 50%;
display: inline-block; // Ensure that element is `inline-block`
&:before { // Ensure that pseudo-before element does not have any WIDTH property
content: "";
display: block;
padding-bottom: 100%;
float: left;
@wklsh
wklsh / _var.scss
Last active June 12, 2020 08:58
Responsive 1vh css variable
// ...
:root {
--vh: 1vh;
}
@wklsh
wklsh / function_component.jsx
Last active June 3, 2020 11:03
SSR / SR Rehydration Rendering
function function_component() {
const hasMounted = useHasMounted();
// has to be declared in function's body prior to usage
// has to be on top to nullify unmounted runs
if (!hasMounted) {
return null;
}
// ...code runs here
var cameraZ = camera.position.z;
var planeZ = 5;
var distance = cameraZ - planeZ;
var aspect = viewWidth / viewHeight;
var vFov = camera.fov * Math.PI / 180;
var planeHeightAtDistance = 2 * Math.tan(vFov / 2) * distance;
var planeWidthAtDistance = planeHeightAtDistance * aspect;
// or
@wklsh
wklsh / ReactCheckUpdatedPropsOrStates.js
Last active November 13, 2018 10:58
Check for updated props / states within ComponentDidUpdate lifecycle
componentDidUpdate(prevProps, prevState) {
// Get updated props
for (const index in prevProps) {
if (prevProps[index] !== this.props[index]) {
console.log(index, prevProps[index], "-->", this.props[index]);
}
}
// Get updated states
for (const index in prevState) {
@wklsh
wklsh / CalMouseDist.js
Last active May 9, 2020 02:39
Calculate Distance Between Mouse and center of Element
/**
* Calculate disance between mouse and center of element
* @param {obj} element target element
* @param {num} mouseX mouse pos X value
* @param {num} mouseY mouse pos Y value
*/
export function calcDistance(element, mouseX, mouseY) {
const elDimensions = element.getBoundingClientRect();
return Math.floor(