<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="//fb.me/react-0.13.3.js"></script>
<meta charset="utf-8">
<title>Index as key</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
body {
margin: 1em;
}
// noprotect
class Item extends React.Component {
render() {
return (
<div className="form-group">
<label className="col-xs-4 control-label">{this.props.name}</label>
<div className="col-xs-8">
<input type='text' className='form-control' />
</div>
</div>
)
}
}
class Example extends React.Component {
constructor() {
super();
this.state = {
list: [
{name: 'Foo1444610101010', id: 1444610101010},
{name: 'Bar1444600000000', id: 1444600000000}
]
};
}
userInput() {
const firstItems = Array.from(document.querySelectorAll('.form-group:first-child input'));
firstItems.forEach((item) => item.value = 'It is ' + (new Date()).toTimeString())
}
addItem() {
const id = +new Date;
this.setState({
list: [ {name: 'Baz' + id, id} , this.state.list]
});
}
render() {
return (
<div>
<b>How to use: </b>
First write something in the inputs
(or <a href='#' onClick={this.userInput}>simulate it</a>).
Then hit <em>Add item</em> and see what happens…
<hr/>
<button className='btn btn-primary' onClick={this.addItem.bind(this)}><b>Add item</b> to the beginning of the list</button>
<h3>Dangerous <code>key=index</code></h3>
<form className="form-horizontal">
{this.state.list.map((todo, index) =>
<Item {todo}
key={index} />
)}
</form>
<h3>Better <code>key=id</code></h3>
<form className="form-horizontal">
{this.state.list.map((todo) =>
<Item {todo}
key={todo.id} />
)}
</form>
<hr/>
<a href='https://medium.com/p/e0349aece318'>« Back to the article</a>.
</div>
)
}
}
React.render(<Example />, document.getElementById('app'))
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |