Skip to content

Instantly share code, notes, and snippets.

View danieldunderfelt's full-sized avatar

Daniel Dunderfelt danieldunderfelt

View GitHub Profile
@danieldunderfelt
danieldunderfelt / MdxContent.js
Created July 10, 2019 05:49
MDX in React-native
// Use your MDX content with this component.
import React from 'react'
import MDX from '@mdx-js/runtime'
import components from '../utils/markdown/markdown'
// Renders a cimple loading spinner as a test
import Loading from './Loading'
const mdxComponents = {
@danieldunderfelt
danieldunderfelt / initializeFormState.js
Created May 26, 2018 05:03
Updated initial state with getDerivedStateFromProps with React 16.4 compatibility.
import * as reduce from 'lodash/reduce'
import * as get from 'lodash/get'
import * as isMatch from 'lodash/isMatch'
export default (nextProps, state) => {
if (nextProps.mutationLoading) {
return null
}
const prevProps = get(state, '_prevProps', false)
// Future versions of Hyper may add additional config options,
// which will not automatically be merged into this file.
// See https://hyper.is#cfg for all currently supported options.
module.exports = {
config: {
// Choose either "stable" for receiving highly polished,
// or "canary" for less polished but more frequent updates
updateChannel: 'canary',
@danieldunderfelt
danieldunderfelt / historyrouter.js
Created May 5, 2017 06:41
A router based on hashrouter that uses the history api where available.
/**
This router depends on the history package:
yarn add history
Simple router for switching between routes. Based on
[hashrouter.js](https://gist.github.com/danieldunderfelt/4d016333a947764c1a5687d26d48ca27)
Suitable for MobX/Redux architecture and especially
handy for tabs, which is the reason why it exists.
@danieldunderfelt
danieldunderfelt / hashrouter.js
Created March 1, 2017 20:24
Simple hash router
/**
* Simple router for switching between hash stings.
* Suitable for MobX/Redux architecture and especially
* handy for tabs, which is the reason why it exists.
const state = { currentTab: 'lions' }
function activateTab(tabName) {
// Dispatch state change
state.currentTab = tabName
### Keybase proof
I hereby claim:
* I am danieldunderfelt on github.
* I am ddunderfelt (https://keybase.io/ddunderfelt) on keybase.
* I have a public key whose fingerprint is 5548 BD67 2A79 92E1 530F 1A25 46CF C630 C47E 05E9
To claim this, I am signing this object:
@danieldunderfelt
danieldunderfelt / StoreContainer.js
Created October 28, 2016 15:36
next.js global store that persists between routes
/**
* PROBLEM:
* In next.js, each route points to a distinct top-level component.
* Since there is no higher-level components in next.js, how to have
* a store that persists between route changes is not immediately
* obvious. One solution is this: a higher-order component that you
* wrap each of your top-level route components with.
*
* This is borrowed from next.js' Redux example from here:
* https://github.com/zeit/next.js/wiki/Redux-example
@danieldunderfelt
danieldunderfelt / index.js
Created June 12, 2016 16:10
Serializing and hydrating state with MobX
/* App creator */
import { extendObservable } from 'mobx'
import AppStore from '../app/AppStore'
export default (initialState = false) => {
const stores = {
App: AppStore
}
@danieldunderfelt
danieldunderfelt / gist:c73745717bc0a7dabc5c
Last active August 29, 2015 14:25
PHP retrieve array key safely, while waiting for the ?? operator
function safe($array)
{
return function($key, $fallback) use ($array)
{
if(!is_array($key))
{
return isset($array[$key]) ? $array[$key] : $fallback;
}
if(!isset($array[$key[0]]))
@danieldunderfelt
danieldunderfelt / randomArray.js
Created December 3, 2014 12:21
Generate an array with a random length, with distinct random values.
/**
*
* Usage: generateArray(10)
* Where 10 is the maximum allowed length (and value range) of the array.
*
* Example output: [5, 3, 9, 1, 0, 2]
*
*
*/