Jump To Right Section
Show
React is a free and open-source front-end JavaScript library for building user interfaces based on components, developed by Facebook Developer. It is maintained by Meta and a community of individual developers and companies. React can be used to develop single-page, mobile, or server-rendered applications with frameworks like Next.js. Let’s start React Cheat Sheet.
Using components
1 2 3 4 5 |
var Component = React.createClass({ ... }); var compo = React.render(<Component />, mountnode); |
Before Rendering
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
React.createClass({ Â Â componentWillMount: function () { Â Â Â Â $.get(this.props.url, function (data) { Â Â Â Â Â Â this.setState(data); Â Â Â Â }); Â Â }, Â Â render: function () { Â Â Â Â return ( Â Â Â Â Â Â <CommentList data={this.state.data} /> Â Â Â Â ); Â Â } }); |
Default Properties
1 2 3 4 5 6 7 |
React.createClass({ Â Â getDefaultProps: function () { Â Â Â Â return {name: ''}; Â Â } ); |
Initial States
1 2 3 4 5 6 7 8 9 10 11 12 13 |
React.createClass({ Â Â getInitialState: function () { Â Â Â Â return {data: []}; Â Â }, Â Â render: function () { Â Â Â Â return ( Â Â Â Â Â Â <CommentList data={this.state.data} /> Â Â Â Â ); Â Â } }); |
Lifecycle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
componentWillMount() componentDidMount() // not on initial render componentWillReceiveProps(props) shouldComponentUpdate(props, state) componentWillUpdate(props, state) componentDidUpdate(prevProps, prevState) componentWillUnmount() // ...there is no DidUnmount or ReceiveState. |
Methods
1 2 3 4 5 6 7 8 9 10 11 |
{ Â Â render: function() Â Â getInitialState: function() Â Â getDefaultProps: function() Â Â mixins: [] Â Â propTypes: {} /<em> for validation </em>/ Â Â statics: { .. } /<em> static methods </em>/ Â Â displayName: '..' /<em> automatically filled in by jsx </em>/ } |
API
1 2 3 4 5 6 |
c.getDOMNode() // deprecated 0.13 React.findDOMNode(c) // 0.13+ c.forceUpdate() c.isMounted() |
Properties
1 2 3 4 5 |
this.setProps({ fullscreen: true }); this.props.fullscreen === true this.replaceProps({ ... }); |
States
1 2 3 4 5 |
this.setState({ editing: true }); this.state.editing === true this.replaceState({ ... }); |
Basic class
1 2 3 4 5 6 7 8 9 |
React.createClass({ Â Â render: function () { Â Â Â Â return ( Â Â Â Â Â Â <div>Hello {this.props.name}</div> Â Â Â Â ); Â Â } }); |
Actions
1 2 3 4 5 6 7 8 9 10 11 12 |
<form onSubmit={this.handleSubmit}>   Name: <input ref="name"> </form> React.createClass({   handleSubmit: function (event) {     name = this.refs['name'].getDOMNode().value;     // see two-way binding below   } }) |
Two-way binding
1 2 3 4 5 6 7 8 9 10 11 12 13 |
React.createClass({ Â Â mixins: [React.addons.LinkedStateMixin], Â Â getInitialState: function() { Â Â Â Â return {value: 'Hello!'}; Â Â }, Â Â render: function() { Â Â Â Â return <input type="text" valueLink={this.linkState('value')} />; Â Â } }); // LinkedStateMixin adds a method to your React component called // linkState(). |
Lists
1 2 3 4 5 6 7 8 9 10 |
var TodoList = React.createClass({ Â Â render: function() { Â Â Â Â var createItem = function(itemText) { Â Â Â Â Â Â return <li>{itemText}</li>; Â Â Â Â }; Â Â Â Â return <ul>{this.props.items.map(createItem)}</ul>; Â Â } }); |
Property validaÂtions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
React.createClass({   propTypes: {     // required     requiredFunc: React.PropTypes.func.isRequired,     requiredAny: React.PropTypes.any.isRequired,     // primitives, optional by default     bool: React.PropTypes.bool,     func: React.PropTypes.func,     number: React.PropTypes.number,     string: React.PropTypes.string,   } }); |
Class set
1 2 3 4 5 6 7 8 9 10 11 12 |
render: function() {   var cx = React.addons.classSet;   var classes = cx({     'message': true,     'message-important': this.props.isImportant,     'message-read': this.props.isRead   });   // same final string, but much cleaner   return <div className={classes}>Great Scott!</div>; } |
PropagÂating properties to children
1 2 3 4 5 6 7 8 9 |
var VideoPlayer = React.createClass({ Â Â render: function() { Â Â Â Â return <VideoEmbed {...this.props} controls='false' />; Â Â } }); <VideoPlayer src="video.mp4" /> |
Mixins
1 2 3 4 5 6 7 8 9 |
var TickTock = React.createClass({ Â Â mixins: [SetIntervalMixin] } SetIntervalMixin = { Â Â componentWillMount: function() { .. } } |
Leave a Comment