Created
February 21, 2019 11:00
-
-
Save Kineolyan/877d26fe04c0ddd0f5393788d087f5c7 to your computer and use it in GitHub Desktop.
React without using setState
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> | |
<script src="https://unpkg.com/[email protected]/umd/react.production.min.js"></script> | |
<script src="https://unpkg.com/[email protected]/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