Skip to content

Instantly share code, notes, and snippets.

@goldhand
goldhand / React Props destrcutre .js
Created May 17, 2016 16:34
Really sweet way of structuring property naming
import React, {Component, PropTypes} from 'react';
export default class GalleryItem extends Component {
static propTypes = {
src: PropTypes.string,
}
render() {
return (
@goldhand
goldhand / loading large images.js
Created May 19, 2016 14:53
An idea for loading large image files. Shouldn't update the state in componentDidMount though. Worried loading the white bg then loading another image right away will lead to thrashing
// currentBG = 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs='; // single white pixel
componentDidMount() {
// set background image after compnent has been built
this.setState({
...this.state,
styles: {
...this.state.styles,
@goldhand
goldhand / ObjectArray.js
Created May 24, 2016 18:51
New javascript generator example
/**
* Helpers for breaking objects into arrays
*/
// returns a generator of the objects keys
export function *generateKeys(obj) {
let prop;
for (prop of obj) {
yield prop;
}
@goldhand
goldhand / connectWindow.js
Last active May 25, 2016 22:14
High order component for listening to window load event and updating a redux store
import React, {Component, PropTypes} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import * as windowActions from 'actions/windowMonitorActions';
/**
* connectWindow is a high order component that will update the windowMonitor
* store once the client window has loaded and connect decorated component's
jest.unmock('../components/Counter');
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import Counter from '../components/Counter';
describe('Counter Component', () => {
{
"name": "testjest",
"version": "0.1.0",
"description": "",
"main": "src/index.js",
"scripts": {
"testold": "./node_modules/.bin/mocha --compilers js:babel-register src/**/__tests__/**/*test*.js",
"test": "NODE_ENV=test jest --no-cache",
"webpack": "NODE_ENV=production ./node_modules/.bin/webpack --progress",
"build": "npm run webpack",
@goldhand
goldhand / :nth-child React Component Factory .js
Created June 9, 2016 19:56
Factory for creating React components with nth-child css selector like properties.
import React, {Component, PropTypes} from 'react';
/**
* createNthChild is a factory for NthChild components.
*
* Can set conditional styling meant to simulate css's nth-child pseudo selector
*
* @param {object} [options] - styles options to pass into the icon panel
* @param {function} [styleFirst] - if isFirst, invoke styleLast(options.styles)
@goldhand
goldhand / ConnectResize.js
Created June 15, 2016 19:29
Flexbox Grid with enhancements implemented in React + Redux.
import React, {Component, PropTypes} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import * as resizeActions from 'actions/windowMonitorActions';
import * as styleOptions from 'constants/styleOptions';
/**
* High order component for connecting components to resize events
@goldhand
goldhand / spawn.js
Last active January 8, 2019 22:18
Use a generator to make async stuff look sync.
/**
* Turns generators into async demons
*
* Within the generator function, any "yield" operator becomes enhanced with async powers
* allowing them to yield promises.
*
* @example
* spawn(function *() {
* const data = yield fetch('/foo.json'); // will be resolved and assigned to "data" var
* console.log(data); // write like its sync but its acually async ;)