Skip to content

Instantly share code, notes, and snippets.

@Kineolyan
Created February 21, 2019 11:00
Show Gist options
  • Save Kineolyan/877d26fe04c0ddd0f5393788d087f5c7 to your computer and use it in GitHub Desktop.
Save Kineolyan/877d26fe04c0ddd0f5393788d087f5c7 to your computer and use it in GitHub Desktop.
React without using setState
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@16.5.1/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16.5.1/umd/react-dom.production.min.js"></script>
<title>Tests</title>
<meta charset="UTF-8">
</head>
<body>
<div id="app"></div>
<script type="text/javascript">
var el = React.createElement;
function Inner(props) {
return el('div', {}, "Hello " + props.name);
}
function Btn(props) {
return el(
'button',
{onClick: props.click},
props.label
);
}
function App(props) {
var that = this;
function changeName(name) {
props.change('name', name);
}
return el(
'div',
{},
[
el(
Btn,
{
click: changeName.bind(null, 'Alice'),
label: 'Alice'
},
[]),
el(
Btn,
{
click: changeName.bind(null, 'Bob'),
label: 'Bob'
},
[]),
el(
Inner,
{name: props.state.name})
]);
}
var s = {
state: {
name: 'Alice'
},
change(key, value) {
s.state[key] = value;
s.render();
},
render() {
ReactDOM.render(
el(App, s),
document.getElementById('app'));
}
};
s.render();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment