Skip to content

Instantly share code, notes, and snippets.

@novacrazy
Last active October 25, 2015 01:58
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 novacrazy/5cd14202a18d09a9ae4e to your computer and use it in GitHub Desktop.
Save novacrazy/5cd14202a18d09a9ae4e to your computer and use it in GitHub Desktop.
Example
import * as React from 'react';
class MyWidget extends React.Component {
static displayName = 'MyWidget';
static propTypes = {
itemUrl: React.PropTypes.string.isRequired,
name: React.PropTypes.string.isRequired
};
//initial state
state = {
clicks: 0
};
onClick(event) {
event.preventDefault();
const {clicks} = this.state;
this.setState({clicks: clicks + 1});
}
render() {
const {itemUrl, name} = this.props;
const {clicks} = this.state;
return (
<div>
<a href={itemUrl} onClick={this.onClick.bind(this)}>{name}</a>
(Clicked {clicks} time{clicks > 1 ? 's' : ''})
</div>
);
}
}
import {List} from 'immutable';
import {Phone} from './someFileWithPhoneDeclared.js';
class MyComponent extends React.Component {
... //other stuff
state = {
phones: new List()
};
onAddNewPhoneClick(event) {
event.preventDefault();
const {phones} = this.state;
this.setState({
phones: phones.push(new Phone()) //Immutable.js returns a NEW list with the pushed value
});
}
... //other stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment