Skip to content

Instantly share code, notes, and snippets.

@tadjik1
Created April 14, 2016 20:47
Show Gist options
  • Save tadjik1/bf9cd8241fa102ea5e13f22dc71fb71a to your computer and use it in GitHub Desktop.
Save tadjik1/bf9cd8241fa102ea5e13f22dc71fb71a to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { render } from 'react-dom';
import { times, uniqueId } from 'lodash';
class Fruite {
constructor(name) {
this.id = uniqueId();
this.name = name;
}
get name() {
return this.name;
}
set name(name) {
if (typeof name === 'string' && string.length > 0) {
this.name = name;
} else {
throw new Error('invalid name');
}
}
}
class App extends Component {
constructor(props) {
super(props);
this.state = {
fruits: props.fruits
};
}
renderFruite(fruite) {
return (
<div className="fruite" key={fruite.id}>
{fruite.name}
</div>
);
}
render() {
const { fruites } = this.state;
return (
<div className="fruites-container">
{fruites.map(renderFruite)}
</div>
);
}
}
const fruits = [
'apple',
'banana',
'pineapple',
'orange',
'pear',
'peach'
].map(name => new Fruite(name));
render(
<App fruites={fruites} />,
document.getElementById('app')
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment