Skip to content

Instantly share code, notes, and snippets.

@davidblurton
Created October 12, 2014 15:21
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 davidblurton/7797b14aeef5165f5dee to your computer and use it in GitHub Desktop.
Save davidblurton/7797b14aeef5165f5dee to your computer and use it in GitHub Desktop.
// Component is a collection of links that have a viewing and editing state.
var Link = React.createClass({
mixins: [React.addons.LinkedStateMixin],
getInitialState: function() {
return {
title: this.props.title, // Copy props to state so we can use LinkedStateMixin. This feels like a hack.
description: this.props.description
};
},
editing: function() {
this.setState({
editing: true
});
},
render: function() {
var editing = (
<form>
<input type="text" valueLink={this.linkState('title')} />
<input type="text" valueLink={this.linkState('description')} />
</form>
);
var viewing = (
<div>
<h3>{this.state.title}</h3>
<p>{this.state.description}</p>
</div>
);
return (
<div>
{this.state.editing ? editing : viewing }
</div>
);
}
});
var Links = React.createClass({
render: function () {
return (
{this.props.links.map(function(link) {
return (
<Link {...link} /> // Pass title and description as props using spread attributes.
);
}, this)}
);
}
});
var links = [{title: 'title', description: 'description'}, etc]
React.renderComponent({links: links}, this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment