Skip to content

Instantly share code, notes, and snippets.

@jquense
jquense / 0. intro.md
Last active September 24, 2022 05:10
Alternative ways to define react Components

The 0.13.0 improvements to React Components are often framed as "es6 classes" but being able to use the new class syntax isn't really the big change. The main thing of note in 0.13 is that React Components are no longer special objects that need to be created using a specific method (createClass()). One of the benefits of this change is that you can use the es6 class syntax, but also tons of other patterns work as well!

Below are a few examples creating React components that all work as expected using a bunch of JS object creation patterns (https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&%20object%20prototypes/ch4.md#mixins). All of the examples are of stateful components, and so need to delegate to React.Component for setState(), but if you have stateless components each patterns tends to get even simpler. The one major caveat with react components is that you need to assign props and context to the component instance otherwise the component will be static. The reason is

@jquense
jquense / entry.js
Created May 17, 2015 21:18
webpack with globalize
var Globalize = require('globalize')
@jquense
jquense / error-style.css
Last active August 29, 2015 14:21
Code mirror jsx mode
.cm-s-monokai .cm-tag.cm-error,
.cm-s-monokai .cm-bracket.cm-error {
color: #f92672;
background: transparent;
}
@jquense
jquense / jsx-mode.js
Last active October 17, 2015 05:40
An attempt at a jsx CodeMirror mode
/*global CodeMirror */
function indexOf(string, pattern, from) {
if (typeof pattern === "string") {
var found = string.indexOf(pattern, from);
return found;
}
var m = pattern.exec(from ? string.slice(from) : string);
return m ? (m.index + from) : -1;
}
function fetchSiteStatus(){
//pretend this is being fetched from a server
return {
type: 'FETCH_SITE_STATUS'
payload: { currentStatus: 'awesome', currentUser: 'John', lastLogin: new Date() }
}
}
function fetchStatuses(){
@jquense
jquense / app.js
Last active August 29, 2015 14:26
implementation of redux's combineReducers adding flux waitFor()
import { createStore } from 'redux';
import combineReducers from './combineReducers';
let Store = createStore(combineReducers({
reducerA(state = 0, action, waitFor){
if ( action.type === 'TEST'){
waitState = waitFor(waitFor.reducerB)
if ( waitState.reducerB === 5 )
state = 10
@jquense
jquense / App.js
Created August 24, 2015 20:00
double react-dom
import React from 'react';
import { render, findDOMNode } from 'react-dom'
render(<div/>, document.getElementById('root'))
@jquense
jquense / routing.js
Created September 14, 2015 04:56
React router with areas and names
import React from 'react';
import { Router as BaseRouter } from 'react-router';
import { getParamNames, formatPattern } from 'react-router/lib/PatternUtils';
import { createRoutes } from 'react-router/lib/RouteUtils'
import createBrowserHistory from 'history/lib/createBrowserHistory';
import useBeforeUnload from 'history/lib/useBeforeUnload';
import invariant from 'app/utils/invariant';
import { each, pick, omit, sortBy } from 'lodash';
const EMPTY_AREA = '@@none';
@jquense
jquense / react-widgets-virtual-list.js
Created September 25, 2015 11:19
Custom List Component
import React from 'react';
import ListMovementMixin from 'react-widgets/lib/mixins/ListMovementMixin';
import List from 'react-widgets/lib/List';
import ListOption from 'react-widgets/lib/ListOption';
import { dataText } from 'react-widgets/lib/util/dataHelpers';
import cn from 'classnames';
import BaseVirtualList from 'react-list';
let VirtualList = React.createClass({
class Tab {
static childContextTypes = {
registerHook: PropTypes.func
}
constructor() {
super()
this._hooks = []
}