Skip to content

Instantly share code, notes, and snippets.

View selbekk's full-sized avatar
👨
Working dad

Kristofer Giltvedt Selbekk selbekk

👨
Working dad
View GitHub Profile
@selbekk
selbekk / query-string.js
Created April 12, 2017 07:28
object to query string
// Implementation
const toQueryString = params => Object.entries(params)
.map((param) => param.join('='))
.join('&');
// Example usage
const qs = toQueryString({ apiKey: 'secret', page: 2 });
qs === 'apiKey=secret&page=2'
@selbekk
selbekk / readme.md
Last active May 29, 2017 07:35
A simple front end build stack

A simple front end build stack

Here's a very bare bones approach.

First, create a webpack.config.js file. This is what goes in:

const config = {
  entry: 'src/index.js', // path to your app entry point
 output: {
@selbekk
selbekk / withBreakpoint.js
Created July 19, 2017 11:54
HOC for fetching breakpoint
import React, { Component } from 'react';
import debounce from 'lodash.debounce';
const withBreakpoint = TargetComponent => class extends Component {
state = {
breakpoint: this.getBreakpoint(),
};
componentDidMount() {
window.addEventListener('resize', this.updateBreakpoint);
@selbekk
selbekk / ScrollToTop.js
Created July 20, 2017 06:33
Scroll to top after react-router navigation
import { PureComponent } from 'react';
import { withRouter } from 'react-router-dom';
class ScrollToTop extends PureComponent {
componentDidUpdate = ({ location }) => location !== this.props.location && window.scrollTo(0, 0);
render = () => null;
}
export default withRouter(ScrollToTop);
@selbekk
selbekk / universal.js
Created July 27, 2017 10:57
Universal server rendering
const path = require('path');
const fs = require('fs');
const React = require('react');
const { renderToString } = require('react-dom/server');
const { StaticRouter } = require('react-router-dom');
const { ServerStyleSheet } = require('styled-components');
const { default: App } = require('../src/containers/App');
@selbekk
selbekk / globalStyles.js
Created August 12, 2017 10:21
Global styles
import normalize from 'styled-normalize';
export default () => `
${normalize}
@import url('https://fonts.googleapis.com/css?family=Open+Sans');
html {
box-sizing: border-box;
}
@selbekk
selbekk / server.js
Created August 18, 2017 12:39
Super simple server side rendering with React and styled-components
const express = require('express');
require('babel-register')({
ignore: /\/(build|node_modules|.svg)\//,
presets: ['env', 'react-app']
});
const universal = require('./universal');
const app = express();
@selbekk
selbekk / action.sublime-snippet
Created August 21, 2017 11:38
Sublime snippet for action and matching action creator
<snippet>
<content><![CDATA[
export const ${1:ACTION_NAME} = '${1}';
export const ${1/(?<=[^\W_])_+([^\W_])|([^\W_]+)|_+/\U$1\L$2/g} = ${2:opts} => ({
type: ${1},${3:}
});
]]></content>
<tabTrigger>action</tabTrigger>
</snippet>
@selbekk
selbekk / action-utils.js
Created February 5, 2018 10:26
Action creator creators!
import Case from 'case';
export const createAction = name => ({
[Case.constant(name)]: Case.constant(name),
[Case.camel(name)]: payload => ({
type: Case.constant(name),
payload,
}),
});
export const createNetworkAction = name => ({
@selbekk
selbekk / registerServiceWorker.js
Created March 16, 2018 10:27
A service worker with refresh dispatching
import { showSnackbar } from './actions';
const isLocalhost = Boolean(
window.location.hostname === 'localhost' ||
// [::1] is the IPv6 localhost address.
window.location.hostname === '[::1]' ||
// 127.0.0.1/8 is considered localhost for IPv4.
window.location.hostname.match(
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
)
);