Skip to content

Instantly share code, notes, and snippets.

View ronnycoding's full-sized avatar
🇻🇪
The only way to do great work is to love what you do.

Ronny Freites ronnycoding

🇻🇪
The only way to do great work is to love what you do.
View GitHub Profile
@ronnycoding
ronnycoding / ControlProps3.js
Created July 9, 2021 05:00
React Patterns
// Control Props
// 💯 extract warnings to a custom hook
import * as React from 'react'
import warning from 'warning'
import {Switch} from '../switch'
const callAll = (...fns) => (...args) => fns.forEach(fn => fn?.(...args))
const actionTypes = {
@ronnycoding
ronnycoding / ControlProps2.js
Created July 9, 2021 05:00
React Patterns
// Control Props
// 💯 add a controlled state warning
import * as React from 'react'
import warning from 'warning'
import {Switch} from '../switch'
const callAll = (...fns) => (...args) => fns.forEach(fn => fn?.(...args))
const actionTypes = {
@ronnycoding
ronnycoding / ControlProps.js
Created July 9, 2021 04:59
React Patterns
// Control Props
// 💯 add read only warning
import * as React from 'react'
import warning from 'warning'
import {Switch} from '../switch'
const callAll = (...fns) => (...args) => fns.forEach(fn => fn?.(...args))
const actionTypes = {
// state reducer
// 💯 state reducer action types
import * as React from 'react'
import {Switch} from '../switch'
const callAll = (...fns) => (...args) => fns.forEach(fn => fn?.(...args))
const actionTypes = {
toggle: 'toggle',
// state reducer
// 💯 default state reducer
import * as React from 'react'
import {Switch} from '../switch'
const callAll = (...fns) => (...args) => fns.forEach(fn => fn?.(...args))
function toggleReducer(state, {type, initialState}) {
switch (type) {
@ronnycoding
ronnycoding / PropGetters.js
Created July 9, 2021 04:55
Prop Collections and Getters
// Prop Collections and Getters
// 💯 prop getters
import * as React from 'react'
import {Switch} from '../switch'
const callAll = (...fns) => (...args) => fns.forEach(fn => fn?.(...args))
function useToggle() {
const [on, setOn] = React.useState(false)
@ronnycoding
ronnycoding / FlexibleCompoundComponent.js
Created July 9, 2021 04:53
Flexible Compound Components with context
// Flexible Compound Components with context
// 💯 custom hook validation
import * as React from 'react'
import {Switch} from '../switch'
const ToggleContext = React.createContext()
ToggleContext.displayName = 'ToggleContext'
function Toggle({children}) {
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(
@ronnycoding
ronnycoding / CompoundComponents.js
Last active July 20, 2022 09:11
React Patterns
// Compound Components
// 💯 Support non-toggle children
// http://localhost:3000/isolated/final/02.extra-1.js
import * as React from 'react'
import {Switch} from '../switch'
function Toggle({children}) {
const [on, setOn] = React.useState(false)
const toggle = () => setOn(!on)
@ronnycoding
ronnycoding / useAsync.js
Last active May 10, 2022 00:59
Epic React hooks
function asyncReducer(state, action) {
switch (action.type) {
case 'pending': {
return {status: 'pending', data: null, error: null}
}
case 'resolved': {
return {status: 'resolved', data: action.data, error: null}
}
case 'rejected': {
return {status: 'rejected', data: null, error: action.error}