Skip to content

Instantly share code, notes, and snippets.

View jimmycallin's full-sized avatar

Jimmy Callin jimmycallin

  • Backlight
  • Stockholm, Sweden
  • 12:23 (UTC +02:00)
  • X @jimmycallin
View GitHub Profile
@jimmycallin
jimmycallin / react-hooks-form-manager.jsx
Created November 27, 2018 18:06
Sick and bored makes a perfect opportunity to learn React Hooks! Implemented a very simple form state manager with autosave and debounce which covers about 80% of what we need at work.
import React, { useEffect, useState } from "react";
const useAutosave = (handleSave, debounceTimeout) => {
const [mounted, setMounted] = useState(false);
const [formValues, setFormValues] = useState({});
const [lastSaved, setLastSaved] = useState({});
const [submitting, setSubmitting] = useState(false);
useEffect(() => {
const timeout = setTimeout(async () => {
if (!mounted) return setMounted(true);
import React, { Component, useState } from "react";
import "./App.css";
const useStateHistory = initialState => {
const [state, setState] = useState(initialState);
const [stateHistory, setStateHistory] = useState([state]);
const [historyIndex, setHistoryIndex] = useState(null);
const setStateWrapper = localState => {
if (historyIndex !== null) {
return setState(stateHistory[historyIndex]);