Created
October 12, 2014 15:21
-
-
Save davidblurton/7797b14aeef5165f5dee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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