Skip to content

Instantly share code, notes, and snippets.

View rafaelgandi's full-sized avatar

Rafael Gandionco rafaelgandi

View GitHub Profile
@rafaelgandi
rafaelgandi / useSpecialEffect.ts
Created May 25, 2022 10:05
useSpecialEffect Implementation
import React from "react";
export default function useSpecialEffect(
callback: (prevRef: any) => any,
updatables: any = {} // Passing false here means the effect will only run once. Like passing [] to useEffect()
): void {
const prevRef = React.useRef<any>({});
const renderCountRef = React.useRef<number>(0);
prevRef.current.update = (propertyString: string, value: any) => {
const [someValue1, setSomeValue1] = useState(null);
const [someValue2, setSomeValue2] = useState(false);
const anotherValueThatCanChange = "some other value";
const [someValue0, setSomeValue0] = useState(true); // Some state that we don't necessarily want to track changes
// This hook will run on every render... similar to useEffect(()=>{})
useSpecialEffect((prev) => {
if (prev.hasChanged({someValue1}, {anotherValueThatCanChange})) {
// Do stuff if someValue1 or anotherValueThatCanChange is updated
@rafaelgandi
rafaelgandi / WidgetMarkup.js
Last active August 12, 2021 13:13
Simple implementation of markup for Scriptable widgets.
// Code has been moved to: https://github.com/rafaelgandi/WidgetMarkup-Scriptable