Skip to content

Instantly share code, notes, and snippets.

View mike-at-redspace's full-sized avatar
🎧

Mike Vardy mike-at-redspace

🎧
View GitHub Profile
@mike-at-redspace
mike-at-redspace / README.txt
Created February 2, 2023 09:01
DevTools Snippet - Console Save
Usage
console.save(data, [filename])
Data can be a string or just an object, objects are passed through json.stringify() before writing to file.
Filename is optional, defaults to ‘console.json’.
@mike-at-redspace
mike-at-redspace / Easing.js
Created January 15, 2023 09:54
Collection of easing functions
/*
## All functions return a number, which is guaranteed to start at 0 and end at 1
f(t), where t in [0..1]
f(0) -> 0
f(1) -> 1
*/
export const easing = {
@mike-at-redspace
mike-at-redspace / removeDuplicates.js
Last active January 9, 2023 18:29
JS Duplicates
function cheapStringify(object) {
if (typeof object !== 'object' || object === null) {
if (typeof object === 'string') {
return `"${object.replace(/"/g, '\\"')}"`
}
return String(object)
}
let result = '{'
for (const key in object) {
result += `"${key.replace(/"/g, '\\"')}":${stringify(object[key])},`
{
"v": "5.7.4",
"fr": 29.9700012207031,
"ip": 0,
"op": 233.000009490285,
"w": 1920,
"h": 1080,
"nm": "3_24_LOGO_CLOSER 2",
"ddd": 0,
"assets": [
@mike-at-redspace
mike-at-redspace / useDebugValue.md
Created December 15, 2022 18:34
useDebugValue

The useDebugValue hook can be useful in advanced scenarios where you want to provide a custom string representation of a value for the React DevTools profiler. This can be useful for debugging and optimizing performance in complex applications.

For example, suppose you have a custom hook called useFormattedDate that returns the current date and time in a specific format. Here is an example of how this hook could be implemented:

function useFormattedDate() {
  const [date, setDate] = useState(new Date());

  useEffect(() => {
 const interval = setInterval(() => {
@mike-at-redspace
mike-at-redspace / useImperativeHandle.md
Created December 15, 2022 18:02
useImperativeHandle

The useImperativeHandle hook can be useful in advanced scenarios where you need to provide a custom instance to the parent of a functional component that is wrapped in a forwardRef.

For example, suppose you have a TextInput component that is implemented as a functional component and wrapped in a forwardRef so that it can be used like this:

const TextInput = React.forwardRef((props, ref) => {
  // The TextInput component implementation goes here
});

// Use the TextInput component with a ref
@mike-at-redspace
mike-at-redspace / App.jsx
Last active December 14, 2022 18:36
React Worker Example
import React, { useState, useEffect } from 'react'
const useWebWorker = (workerUrl, callback) => {
const [worker, setWorker] = useState(null)
useEffect(() => {
if (workerUrl) {
const newWorker = new Worker(workerUrl)
setWorker(newWorker)
}
@mike-at-redspace
mike-at-redspace / Page.jsx
Last active December 14, 2022 12:41
React Emotion Page
import React from 'react';
import styled from '@emotion/styled';
const FlexboxWrapper = styled.div`
display: flex;
flex-direction: ${props => props.direction || 'row'};
align-items: ${props => props.alignItems || 'stretch'};
justify-content: ${props => props.justifyContent || 'flex-start'};
`;
@mike-at-redspace
mike-at-redspace / App.jsx
Last active November 2, 2022 19:23
useScrollSpy
import React, { useRef } from 'react'
const App = () => {
const sectionRefs = [useRef(null), useRef(null), useRef(null)]
const activeSection = useScrollSpy({
sectionElementRefs: sectionRefs,
offsetPx: -80
})
@mike-at-redspace
mike-at-redspace / less-common.md
Last active January 5, 2023 13:26
Webplex Stuff

ZIM env .zshrc stuff

export CACHE_CLEAR_USERNAME=[user]
export CACHE_CLEAR_PASSWORD=[pass]
export ZIM_DEBUG=development

Username/Password used for CACHE_CLEAR can be found here. This allows for cache resets anytime you append the resetCache=true query param.