Skip to content

Instantly share code, notes, and snippets.

View doctorDAlvarez's full-sized avatar
:atom:
Learning…

Diego Alvarez doctorDAlvarez

:atom:
Learning…
View GitHub Profile
@doctorDAlvarez
doctorDAlvarez / bc.py
Created January 12, 2022 17:06
Custom Blockchain implemented with python3
from hashlib import sha256
import json
import time
class Chain:
def __init__(self):
self.blockchain = []
self.pending = []
self.add_block(prevHash="Genesis", proof=123)
@doctorDAlvarez
doctorDAlvarez / useAutoScroll.js
Created August 9, 2021 21:42
tiny custom sideeffect hook to acomodate autoscroll.
function useAutoScroll(ref) {
useEffect(() => {
const node = ref.current;
node.scrollTop = node.scrollHeight;
});
}
@doctorDAlvarez
doctorDAlvarez / useAuth.js
Created August 9, 2021 01:52
custom hook made to handle users with firebase, so cool!
function useAuth() {
const [user, setUser] = useState(null);
useEffect(() => {
return firebase.auth().onAuthStateChanged((user) => {
if (user) {
setUser({
displayName: user.displayName,
photoUrl: user.photoURL,
uid: user.uid,
@doctorDAlvarez
doctorDAlvarez / useCollection.js
Created August 9, 2021 01:11
custom hook to operate on a firebase database. accepts endpoint and orderby for now.
function useCollection(path, orderBy) {
const [docs, setDocs] = useState([]);
useEffect(() => {
return db
.collection(path)
.orderBy(orderBy)
.onSnapshot((snapshot) => {
const docs = [];
snapshot.forEach((doc) => {
@doctorDAlvarez
doctorDAlvarez / useFeedExample.js
Created August 5, 2021 00:41
Nice cool implementation of a post feed using useReducer. Thanks @ryanflorence and @mjackson for the workshop
import React from "react"
import FeedPost from "app/FeedPost"
import { loadFeedPosts, subscribeToNewFeedPosts } from "app/utils"
export default Feed
const PER_PAGE = 3;
function Feed() {
@doctorDAlvarez
doctorDAlvarez / useControlSwitchWarning.js
Created August 1, 2021 03:45
Cool custom hook learned from reach-ui lib.
/**
* Logs a warning in dev mode when a component switches from controlled to
* uncontrolled, or vice versa
*
* A single prop should typically be used to determine whether or not a
* component is controlled or not.
*
* @param controlPropValue
* @param controlPropName
* @param componentName
@doctorDAlvarez
doctorDAlvarez / useInterval.js
Created July 29, 2021 21:26
very usefull custom hook to use set interval effectively. Thanks Dan!
// Property of @dan_abramov. Very helpfull explanation of the mental model to change imperative APIs to more
// declarative custom hooks.
import React, { useState, useEffect, useRef } from 'react';
function useInterval(callback, delay) {
const savedCallback = useRef();
// Remember the latest callback.
useEffect(() => {
@doctorDAlvarez
doctorDAlvarez / useSafeContext.js
Created July 20, 2021 04:25
Simple custom hook to consume context safely.
import React from 'react'
function useSafeContext(customContext) {
const context = React.useContext(customContext)
if (!context) {
throw new Error('You must use this context consumer hook inside a Provider')
}
return context
}
@doctorDAlvarez
doctorDAlvarez / useAsync.js
Last active July 19, 2021 21:59
custom API for async calls mgmt
import * as React from 'react'
function useSafeDispatch(dispatch) {
const mounted = React.useRef(false)
React.useLayoutEffect(() => {
mounted.current = true
return () => {
mounted.current = false
}
@doctorDAlvarez
doctorDAlvarez / useLocalStorageState.js
Created July 18, 2021 20:42
localStorage API custom hook
import * as React from 'react'
/**
*
* @param {String} key The key to set in localStorage for this value
* @param {Object} defaultValue The value to use if it is not already in localStorage
* @param {{serialize: Function, deserialize: Function}} options The serialize and deserialize functions to use (defaults to JSON.stringify and JSON.parse respectively)
*/
function useLocalStorageState(