made with esnextbin
Last active
October 9, 2016 17:03
-
-
Save wtfil/d11e4325bac31a897fbddc9b27cc4daa to your computer and use it in GitHub Desktop.
esnextbin sketch
This file contains hidden or 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
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>ESNextbin Sketch</title> | |
</head> | |
<body> | |
<div class="app"></div> | |
</body> | |
</html> |
This file contains hidden or 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
import React, {Component} from 'react'; | |
import {render} from 'react-dom'; | |
class Form extends Component { | |
constructor() { | |
super(); | |
this.state = { | |
email: '', | |
password: '' | |
}; | |
} | |
onSubmit(e) { | |
e.preventDefault(); | |
fetch('http://example.com/api/login', { | |
method: 'post', | |
body: JSON.stringify({ | |
email: this.state.email, | |
password: this.state.password, | |
}) | |
}).then(res => { | |
if(res.status === 200) { | |
console.log('ok'); | |
} else { | |
throw new Error('Not authorized') | |
} | |
}).catch(err => { | |
console.log('error', error); | |
}) | |
} | |
render() { | |
return <form onSubmit={this.onSubmit.bind(this)}> | |
<input value={this.state.email} onChange={e => this.setState({email: e.target.value})}/> | |
<input value={this.state.password} onChange={e => this.setState({password: e.target.value})}/> | |
<button type='submit'>Login</button> | |
</form> | |
} | |
} | |
render(<Form/>, document.querySelector('.app')); |
This file contains hidden or 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
{ | |
"name": "esnextbin-sketch", | |
"version": "0.0.0", | |
"dependencies": { | |
"react": "15.3.2", | |
"react-dom": "15.3.2", | |
"babel-runtime": "6.11.6" | |
} | |
} |
This file contains hidden or 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
'use strict'; | |
var _stringify = require('babel-runtime/core-js/json/stringify'); | |
var _stringify2 = _interopRequireDefault(_stringify); | |
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); | |
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); | |
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | |
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | |
var _createClass2 = require('babel-runtime/helpers/createClass'); | |
var _createClass3 = _interopRequireDefault(_createClass2); | |
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); | |
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | |
var _inherits2 = require('babel-runtime/helpers/inherits'); | |
var _inherits3 = _interopRequireDefault(_inherits2); | |
var _react = require('react'); | |
var _react2 = _interopRequireDefault(_react); | |
var _reactDom = require('react-dom'); | |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
var Form = function (_Component) { | |
(0, _inherits3.default)(Form, _Component); | |
function Form() { | |
(0, _classCallCheck3.default)(this, Form); | |
var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(Form).call(this)); | |
_this.state = { | |
email: '', | |
password: '' | |
}; | |
return _this; | |
} | |
(0, _createClass3.default)(Form, [{ | |
key: 'onSubmit', | |
value: function onSubmit(e) { | |
e.preventDefault(); | |
fetch('http://example.com/api/login', { | |
method: 'post', | |
body: (0, _stringify2.default)({ | |
email: this.state.email, | |
password: this.state.password | |
}) | |
}).then(function (res) { | |
if (res.status === 200) { | |
console.log('ok'); | |
} else { | |
throw new Error('Not authorized'); | |
} | |
}).catch(function (err) { | |
console.log('error', error); | |
}); | |
} | |
}, { | |
key: 'render', | |
value: function render() { | |
var _this2 = this; | |
return _react2.default.createElement( | |
'form', | |
{ onSubmit: this.onSubmit.bind(this) }, | |
_react2.default.createElement('input', { value: this.state.email, onChange: function onChange(e) { | |
return _this2.setState({ email: e.target.value }); | |
} }), | |
_react2.default.createElement('input', { value: this.state.password, onChange: function onChange(e) { | |
return _this2.setState({ password: e.target.value }); | |
} }), | |
_react2.default.createElement( | |
'button', | |
{ type: 'submit' }, | |
'Login' | |
) | |
); | |
} | |
}]); | |
return Form; | |
}(_react.Component); | |
(0, _reactDom.render)(_react2.default.createElement(Form, null), document.querySelector('.app')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment