Skip to content

Instantly share code, notes, and snippets.

@lemonmade
Last active January 3, 2017 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lemonmade/9aa309a74565cab479a87be590416e0d to your computer and use it in GitHub Desktop.
Save lemonmade/9aa309a74565cab479a87be590416e0d to your computer and use it in GitHub Desktop.
To type or not to type
// @flow
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
type TestDetails<T: HTMLElement> = {
node: HTMLElement,
};
type Props = {
children?: React$Element<any>,
onTestReady?: <T: HTMLElement>(details: TestDetails<T>) => void,
};
export default class Tester extends Component {
props: Props;
performTest() {
const {onTestReady, children} = this.props;
if (children == null || onTestReady == null) {
return;
}
onTestReady({node: ReactDOM.findDOMNode(this).children[0]});
}
componentDidMount() {
this.performTest();
}
componentDidUpdate() {
this.performTest();
}
render() {
const {children} = this.props;
if (children == null) { return null; }
return <div>{children}</div>;
}
}
import * as React from 'react';
import {findDOMNode} from 'react-dom';
interface TestDetails<E extends HTMLElement> {
node: E,
}
interface Props {
children?: React.ReactNode,
onReady: <E extends HTMLElement>(details: TestDetails<E>) => void,
}
export default class Tester extends React.Component<Props, {}> {
private performTest() {
const {onReady, children} = this.props;
if (children == null) { return; }
onReady({
node: findDOMNode(this).children[0],
})
}
componentDidMount() {
this.performTest();
}
componentDidUpdate() {
this.performTest();
}
render() {
const {children} = this.props;
if (children == null) { return null; }
return <div>{children}</div>;
}
}
import React, {Component, PropTypes} from 'react';
import {findDOMNode} from 'react-dom';
export default class Tester extends Component {
static propTypes = {
children: PropTypes.node,
onReady: PropTypes.func,
};
performTest() {
const {onReady, children} = this.props;
if (children == null) { return; }
onReady({
node: findDOMNode(this).children[0],
})
}
componentDidMount() {
this.performTest();
}
componentDidUpdate() {
this.performTest();
}
render() {
const {children} = this.props;
if (children == null) { return null; }
return <div>{children}</div>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment