React:
<script src="https://unpkg.com/react@15/dist/react.js"></script>
$ npm install react --save
$ bower install react --save
React DOM:
<script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
$ npm install react-dom
$ bower install react-dom --save
ReactDOM.render(
React.createElement(Link, {name: 'HackHall.com'}),
document.getElementById('menu')
)
ReactDOM.render(
<Link name='HackHall.com'/>,
document.getElementById('menu')
)
var ReactDOMServer = require('react-dom/server')
ReactDOMServer.renderToString(Link, {name: 'HackHall.com'})
ReactDOMServer.renderToStaticMarkup(Link, {name: 'HackHall.com'})
var Link = React.createClass({
displayName: 'Link',
render: function() {
return React.createElement('a', {className: 'btn', title: this.props.name}, 'Click ->', this.props.name);
}
})
var Link = React.createClass({
render: function() {
return <a className='btn' title={this.props.name}>Click -> this.props.name</a>
}
})
class Link extends React.Component {
render() {
return <a className='btn' title={this.props.name}>Click -> this.props.name</a>
}
}
propTypes object
: Type validation in development modegetDefaultProps function()
: object of default propsgetInitialState function()
: object of the initial state
ES5:
var Link = React.createClass ({
propTypes: { name: React.PropTypes.string },
getDefaultProps: function() {
return { initialCount: 0 }
},
getInitialState: function() {
return {count: this.props.initialCount};
},
tick: function() {
this.setState({count: this.state.count + 1});
},
render: function() {
return React.createElement(
'a',
{className: 'btn', href: '#', title: this.props.name, onClick: this.tick.bind(this)},
'Click ->',
(this.props.name? this.props.name : 'webapplog.com'),
' (Clicked: '+this.state.count+')'
)
}
})
ES5 + JSX:
var Link = React.createClass ({
propTypes: { name: React.PropTypes.string },
getDefaultProps: function() {
return { initialCount: 0 }
},
getInitialState: function() {
return {count: this.props.initialCount};
},
tick: function() {
this.setState({count: this.state.count + 1});
},
render: function() {
return (
<a onClick={this.tick.bind(this)} href="#" className="btn" title={this.props.name}>
Click -> {(this.props.name ? this.props.name : 'webapplog.com')}
(Clicked: {this.state.count})
</a>
)
}
})
ES6 + JSX:
export class Link extends React.Component {
constructor(props) {
super(props);
this.state = {count: props.initialCount};
}
tick() {
this.setState({count: this.state.count + 1});
}
render() {
return (
<a onClick={this.tick.bind(this)} href="#" className="btn" title={this.props.name}>
Click -> {(this.props.name? this.props.name : 'webapplog.com')}
(Clicked: {this.state.count})
</a>
);
}
}
Link.propTypes = { initialCount: React.PropTypes.number };
Link.defaultProps = { initialCount: 0 };
Modern React lifecycle methods (v16+)
Legacy Lifecycle Events:
componentWillMount function()
componentDidMount function()
componentWillReceiveProps function(nextProps)
shouldComponentUpdate function(nextProps, nextState)-> bool
componentWillUpdate function(nextProps, nextState)
componentDidUpdate function(prevProps, prevState)
componentWillUnmount function()
Sequence of lifecycle events:
Inspired by http://react.tips
key
: Unique identifier for an element to turn arrays/lists into hashes for better performance, e.g.,key={id}
ref
: Reference to an element viathis.refs.NAME
, e.g.,ref="email"
will createthis.refs.email
DOM node orReactDOM.findDOMNode(this.refs.email)
style
: Accept an object of styles, instead of a string (immutable since v0.14), e.g.,style={{color: red}}
className
: the HTMLclass
attribute, e.g.,className="btn"
htmlFor
: the HTMLfor
attribute, e.g.,htmlFor="email"
dangerouslySetInnerHTML
: raw HTML by providing an object with the key__html
children
: content of the element viathis.props.children
, e.g.,this.props.children[0]
data-NAME
: custom attribute, e.g.,data-tooltip-text="..."
Types available under React.PropTypes
:
any
array
bool
element
func
node
number
object
string
To make required, append .isRequired
.
More methods:
instanceOf(constructor)
oneOf(['News', 'Photos'])
oneOfType([propType, propType])
propTypes: {
customProp: function(props, propName, componentName) {
if (!/regExPattern/.test(props[propName])) {
return new Error('Validation failed!');
}
}
}
Properties:
this.refs
: Lists components with aref
propthis.props
: Any props passed to an element (immutable)this.state
: State set by setState and getInitialState (muttable) — avoid setting state manually withthis.state=...
this.isMounted
: Flag whether the element has a corresponding DOM node or not
Methods:
setState(changes)
: Change state (partially) tothis.state
and trigger re-renderreplaceState(newState)
: Replacethis.state
and trigger re-renderforceUpdate()
: Trigger DOM re-render immediately
As npm modules:
react-addons-css-transition-group
react-addons-perf
react-addons-test-utils
react-addons-pure-render-mixin
react-addons-linked-state-mixin
react-addons-clone-with-props
react-addons-create-fragment
react-addons-css-transition-group
react-addons-linked-state-mixin
react-addons-pure-render-mixin
react-addons-shallow-compare
react-addons-transition-group
react-addons-update
- https://github.com/brillout/awesome-react-components and http://devarchy.com/react-components: List of React components
- Material-UI: Material design React components
- http://react-toolbox.com: Set of React components that implement Google Material Design specification
- https://js.coach: Opinionated catalog of open source JS (mostly React) packages
- https://react.rocks: Catalog of React components
- https://khan.github.io/react-components: Khan Academy React components
- http://www.reactjsx.com: Registry of React components
Source from azat-co