Skip to content

Instantly share code, notes, and snippets.

View sag1v's full-sized avatar
🎯
Focusing

Sagiv ben giat sag1v

🎯
Focusing
View GitHub Profile
@sag1v
sag1v / undoRedo.js
Last active December 3, 2024 22:00
A simple implementation of undo and redo in JavaScript
const stripLast = arr => {
// split the last item from an array and return a tupple of [rest, last]
const length = arr.length;
const lastItem = arr[length - 1];
const restOfArr = arr.slice(0, length - 1);
return [restOfArr, lastItem];
};
const createTrimStartArray = length => arr => {
const startIndex = arr.length < length ? 0 : length;
@sag1v
sag1v / Markdium-JSX.jsx
Last active October 25, 2019 20:43
Markdium-React state update on an unmounted component
function Pets() {
const [pets, dispatch] = useReducer(petsReducer, initialState);
const onChange = ({ target }) => {
dispatch({ type: "PET_SELECTED", payload: target.value });
};
useEffect(() => {
if (pets.selectedPet) {
dispatch({ type: "FETCH_PET" });
@sag1v
sag1v / Markdium-JSX.jsx
Last active October 25, 2019 20:42
Markdium-React state update on an unmounted component
function App() {
return (
<div>
<Pets />
</div>
);
}
@sag1v
sag1v / Markdium-JSX.jsx
Created October 25, 2019 20:00
Markdium-React state update on an unmounted component
const petsDB = {
dogs: { name: "Dogs", voice: "Woof!", avatar: "🐶" },
cats: { name: "Cats", voice: "Miauuu", avatar: "🐱" }
};
export function getPet(type) {
return new Promise(resolve => {
// simulate a fetch call
setTimeout(() => {
resolve(petsDB[type]);
@sag1v
sag1v / Markdium-JSX.jsx
Last active October 25, 2019 20:40
Markdium-React state update on an unmounted component
function Pets() {
const [pets, dispatch] = useReducer(petsReducer, initialState);
const onChange = ({ target }) => {
dispatch({ type: "PET_SELECTED", payload: target.value });
};
useEffect(() => {
if (pets.selectedPet) {
dispatch({ type: "FETCH_PET" });
@sag1v
sag1v / Markdium-JSX.jsx
Last active October 26, 2019 10:45
Markdium-React state update on an unmounted component
function useIsMountedRef(){
const isMountedRef = useRef(null);
useEffect(() => {
isMountedRef.current = true;
return () => isMountedRef.current = false;
});
return isMountedRef;
}
@sag1v
sag1v / Markdium-JSX.jsx
Last active October 25, 2019 20:44
Markdium-React state update on an unmounted component
function Pets() {
const [pets, dispatch] = useReducer(petsReducer, initialState);
const onChange = ({ target }) => {
dispatch({ type: "PET_SELECTED", payload: target.value });
};
useEffect(() => {
let mounted = true;
if (pets.selectedPet) {
@sag1v
sag1v / Markdium-JSX.jsx
Last active October 25, 2019 20:43
Markdium-React state update on an unmounted component
function App() {
const [showPets, setShowPets] = useState(true);
const toggle = () => {
setShowPets(state => !state);
};
return (
<div>
<button onClick={toggle}>{showPets ? "hide" : "show"}</button>
@sag1v
sag1v / Markdium-JSX.jsx
Last active October 25, 2019 20:45
Markdium-React state update on an unmounted component
function Pets() {
const [pets, dispatch] = useReducer(petsReducer, initialState);
const isMountedRef = useRef(null);
const onChange = ({ target }) => {
dispatch({ type: "PET_SELECTED", payload: target.value });
};
useEffect(() => {
isMountedRef.current = true;
@sag1v
sag1v / Markdium-JSX.jsx
Last active October 25, 2019 20:41
Markdium-React state update on an unmounted component
const initialState = { loading: false, selectedPet: "", petData: null }
function petsReducer(state, action) {
switch (action.type) {
case "PET_SELECTED": {
return {
...state,
selectedPet: action.payload
};
}