Skip to content

Instantly share code, notes, and snippets.

View sanfilippopablo's full-sized avatar

Pablo Sanfilippo sanfilippopablo

View GitHub Profile
import { useState, useCallback } from "react";
export default function usePersistedState(key, initialValue) {
// State to store our value
// Pass initial state function to useState so logic is only executed once
const [storedValue, setStoredValue] = useState(() => {
try {
// Get from local storage by key
const item = window.localStorage.getItem(key);
// Parse stored json or if none return initialValue
const isEmpty = value => value === undefined || value === null || value === "";
const join = rules => (value, data) =>
rules.map(rule => rule(value, data)).filter(error => !!error)[0];
export function email(value) {
if (
!isEmpty(value) &&
!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)
) {
return "Invalid e-mail address";
@sanfilippopablo
sanfilippopablo / PostHOCApp.js
Created March 30, 2017 18:39
Use of HOCs to isolate behavior.
import React, { Component } from "react";
import styled from "styled-components";
import SwipeableViews from "react-swipeable-views";
import Nav from "./components/Nav";
import Player from "./components/Player";
import Mensajes from "./components/Mensajes";
const AppContainer = styled.div`
height: 100vh;