Skip to content

Instantly share code, notes, and snippets.

@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)
{
"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",
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', () => {
@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
@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 / 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 / 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 / Deep Object Destructuring es6 .js
Created May 17, 2016 15:39
Example of destructuring deep objects in es6
let katdawg = {
kat: {
says: {
meow: '...'
}
},
dawg: {
bark: 'woof'
}
@goldhand
goldhand / ContextComponent.js
Last active May 19, 2016 18:18
The long version of what connect react-redux method replacing
class ContextComponent extend React.Componet {
static contextTypes = {
store: PropTypes.object,
}
componentDidMount() {
this.unsubscribe = this.context.store.subscribe(() => {
this.forceUpdate();