Skip to content

Instantly share code, notes, and snippets.

@balazimichal
Last active July 9, 2018 07:53
Show Gist options
  • Save balazimichal/2ac79e0878807a509a8b2fdf51954652 to your computer and use it in GitHub Desktop.
Save balazimichal/2ac79e0878807a509a8b2fdf51954652 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Car counter</title>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
class CarCounter extends React.Component {
constructor(props) {
super(props);
this.state = {
cars : [
{id: 1, color: 'red', count: -1},
{id: 2, color: 'green', count: 2},
{id: 3, color: 'blue', count: 3}
],
value : ''
};
this.formChangeHandle = this.formChangeHandle.bind(this);
this.formSubmitHandle = this.formSubmitHandle.bind(this);
this.removeHandler = this.removeHandler.bind(this);
this.addHandler = this.addHandler.bind(this);
this.deductHandler = this.deductHandler.bind(this);
}
removeHandler(c) {
this.setState(prevState => ({
cars : prevState.cars.filter((car) => {
return car.id !== c.id
})
}))
}
addHandler(c) {
this.setState((prevState) => ({
cars: prevState.cars.map((car) => {
if (car.id === c.id) {
car.count = car.count + 1
return car
} else {
return car
}
})
}))
}
deductHandler(c) {
this.setState((prevState) => ({
cars: prevState.cars.map((car) => {
if (car.id === c.id) {
car.count = car.count - 1
return car
} else {
return car
}
})
}))
}
formSubmitHandle(e) {
e.preventDefault();
this.setState({
cars: [...this.state.cars, {id: this.randID(), color: this.state.value, count: 0}],
value: ''
});
}
formChangeHandle(e) {
this.setState({ value: e.target.value})
}
randID() {
return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(2, 10);
}
render() {
return (
<div>
<h1>Car Counter</h1>
<p>Add new car color:</p>
{JSON.stringify(this.state)}
<form onSubmit={this.formSubmitHandle} onChange={this.formChangeHandle}><input type="text" value={this.state.value} /><input type="submit" value="Add" /></form>
<ul>
{
this.state.cars.sort((a, b) => a.count < b.count).map((car) =>
<li key={car.id}>{car.color} (ID:{car.id}) ({car.count}) <button onClick={(e) => {this.addHandler(car)}}>+</button><button onClick={(e) => {this.deductHandler(car)}}>-</button><button onClick={(e) => {this.removeHandler(car)}}>Remove</button></li>
)
}
</ul>
</div>
);
}
}
ReactDOM.render(
<CarCounter />,
document.getElementById('root')
);
</script>
<!--
Note: this page is a great way to try React but it's not suitable for production.
It slowly compiles JSX with Babel in the browser and uses a large development build of React.
To set up a production-ready React build environment, follow these instructions:
* https://reactjs.org/docs/add-react-to-a-new-app.html
* https://reactjs.org/docs/add-react-to-an-existing-app.html
You can also use React without JSX, in which case you can remove Babel:
* https://reactjs.org/docs/react-without-jsx.html
* https://reactjs.org/docs/cdn-links.html
-->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment