Skip to content

Instantly share code, notes, and snippets.

View mjackson's full-sized avatar
💿

Michael Jackson mjackson

💿
View GitHub Profile
@mjackson
mjackson / WrappedText.js
Created June 9, 2016 00:15
Text measuring in React.js
import React from 'react'
import ReactDOM from 'react-dom'
class WrappedText extends React.Component {
static propTypes = {
onResize: React.PropTypes.func
}
handleWindowResize = () => {
if (this.props.onResize) {
@mjackson
mjackson / BrowserRouter.js
Last active December 16, 2016 02:08
The core of react-router
import React from 'react'
import BrowserHistory from 'react-history/BrowserHistory'
import Router from './Router'
const BrowserRouter = (props) => (
<BrowserHistory {...props}>
<Router {...props}/>
</BrowserHistory>
)
## react-router
<Router>
<MemoryRouter>
<Route>
<Switch>
<Redirect>
## react-router-dom
<BrowserRouter>
<HashRouter>
@mjackson
mjackson / WindowScrollTo.js
Last active January 16, 2017 02:11
Make window.scrollTo declarative using a <WindowScrollTo> React component
import React from 'react'
import warning from 'warning'
const { number, object } = React.PropTypes
const WindowScrollTo = React.createClass({
contextTypes: {
windowScrollTo: object
},
const mom = new Date('October 2 1980')
const dad = new Date('April 30 1981')
const kid1 = new Date('September 7 2006')
const kid2 = new Date('July 11 2009')
const kid3 = new Date('May 6 2011')
const family = [ mom, dad, kid1, kid2, kid3 ]
const oneDay = 1000 * 60 * 60 * 24
const oneYear = oneDay * 365
/*eslint-disable no-alert */
////////////////////////////////////////////////////////////////////////////////
// Exercise:
//
// Using context, implement the <Form>, <SubmitButton>, and <TextInput>
// components such that:
//
// - Clicking the <SubmitButton> "submits" the form
// - Hitting "Enter" while in a <TextInput> submits the form
// - Don't use a <form> element, we're intentionally recreating the
// In v2/3 you did this:
import ReactDOM from 'react-dom'
import { Router, browserHistory, Route } from 'react-router'
ReactDOM.render(
<Router>
<Route path="/about" component={About}/>
<Route path="/:username" component={User}/>
</Router>
)
System.config({
transpiler: 'babel',
paths: {
'npm:': 'https://unpkg.com/'
},
map: {
'babel': 'npm:babel-core@5.8.38/browser.min.js',
'react': 'npm:react@15.5.4',
'react-dom': 'npm:react-dom@15.5.4',
////////////////////////////////////////////////////////////////////////////////
// Exercise:
//
// Implement a radio group form control with the API found in <App>.
//
// - Clicking a <RadioOption> should update the value of <RadioGroup>
// - The selected <RadioOption> should pass the correct value to its <RadioIcon>
// - The `defaultValue` should be set on first render
//
// Hints to get started:
import React from 'react'
import invariant from 'invariant'
const isFunction = (object) =>
typeof object === 'function'
const createComponent = (def) => {
invariant(
def,
'createComponent is missing component definition'