Skip to content

Instantly share code, notes, and snippets.

View laytong's full-sized avatar

Layton laytong

View GitHub Profile
@laytong
laytong / use-toggle.jsx
Created December 7, 2020 21:17
Handy reusable toggle hook
import { useState, useCallback } from 'react';
const useToggle = (initialState) => {
const [isOpen, setIsOpen] = useState(initialState);
// Flip whatever is the current state
const toggle = useCallback(
() => setIsOpen(state => !state),
[setIsOpen],
);
@laytong
laytong / keybase.md
Created February 9, 2017 00:34
keybase gist

Keybase proof

I hereby claim:

  • I am laytong on github.
  • I am laytong (https://keybase.io/laytong) on keybase.
  • I have a public key ASCyt6lo0OK-LX3x-ZafvydihUNq1fPyeqqhihrypK3jdQo

To claim this, I am signing this object:

@laytong
laytong / bad-input.jsx
Last active December 18, 2021 06:16
How to debounce your inputs for super fast react/redux components
import React, {Component, PropTypes} from 'react';
class BadInputComponent extends Component {
static propTypes = {
text = PropTypes.string.isRequired,
updateText = PropTypes.func.isRequired,
};
render() {
return (
@laytong
laytong / reducer.js
Created August 30, 2016 23:56
Nested reducer pattern for deeply nested state
const defaultState = {
person: {
name: ''
age: 20,
friends: []
pets: []
}
}
const actionMap = {
@laytong
laytong / complexityReducer.js
Created July 12, 2016 18:33
Remove complexity from redux reducers
const initialObjectivesState = {
aString: '',
};
/*
* As the number of actions increases, the cyclomatic complexity of the reducer will skyrocket
*/
const Reducer = function (state = initialObjectivesState, action) {
switch(action.type){
"UPDATE_STRING": function(state, action){