Skip to content

Instantly share code, notes, and snippets.

View helloncanella's full-sized avatar

Hellon Canella helloncanella

  • Peats GmbH
  • Rio de Janeiro
View GitHub Profile
import useRouter from "use-react-router"
import { matchPath } from "react-router-dom"
export default function useParams(path) {
const { location } = useRouter()
const { pathname } = location
const pattern = `(.*)?${path}`
const match = matchPath(pathname, { path: pattern }) || {}
export default function executeSequentialPromises(funcs) {
return funcs.reduce(
(promise, func) =>
promise.then(result =>
(func() || Promise.resolve(undefined)).then(
Array.prototype.concat.bind(result)
)
),
Promise.resolve([])
)
import _ from "lodash"
/**
* consider the object
*
* const obj = {
* key1: value1
* key2: value2
* "key3,key4,key5": {key3: value3, key4: value4}
* }
*
@helloncanella
helloncanella / index.js
Last active November 1, 2019 03:56
basic-node-config
require = require("esm")(module)
module.exports = require("./server")
import React from "react"
import _ from "lodash"
/**
* Provided that a list of providers [P1, P2, P3, P4] is passed as props,
* it renders
*
* <P1>
<P2>
<P3>
import React, { useState, useContext, useMemo } from "react"
export default function getContextManager() {
const Context = React.createContext()
function Provider(props) {
const [state, setState] = useState()
const api = useMemo(() => [state, setState], [JSON.stringify(state)])
return <Context.Provider value={api}>{props.children}</Context.Provider>
}
@helloncanella
helloncanella / useNewEntries.js
Created October 17, 2019 23:16
useNewEntries.js
import _ from "lodash"
import { useState, useEffect, useRef } from "react"
export default function useNewEntries({ entries }) {
const [newEntries, setNewEntries] = useState([])
const oldEntries = useRef(entries)
useEffect(() => {
if (_.isEmpty(entries)) return
const newEntries = entries.filter(entry =>
import _ from "lodash"
import useRouter from "use-react-router"
import { join } from "path"
import { matchPath } from "react-router-dom"
export default function useMatchPath(path) {
const { location } = useRouter()
const pathname = _.get(location, "pathname")
const match = matchPath(pathname, { path: join("(.*)?", path) }) || {}
const LIST = gql`
query List($category: String!) {
list(category: $category) {
id
description
}
}
`
const [list, setList] = useAppState({
function useCount({ label }) {
const COUNT = gql`
query Count($label: ID) {
count(label: $label) @client
}
`
const [count, setCount] = useAppState({
query: COUNT,
variables: { label }