<body>
<div id="root"></div>
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/[email protected]/babel.js"></script>
<script type="text/babel">
const rootElement = document.getElementById('root');
// The current state of our app
const state = { eventCount: 0, username: '' };
function App() {
function handleClick() {
setState({ eventCount: state.eventCount + 1 });
}
function handleChange(event) {
// Getting current value from the input
console.log(event);
// Getting the SyntheticEvent
console.log(event.nativeEvent);
// Getting the nativeEvent if needed
setState({ username: event.target.value });
}
return (
<div>
<p>There have been {state.eventCount} events.</p>
<p>
{/* We can have different types of events here that are supported by React: https://reactarmory.com/guides/react-events-cheatsheet */}
<button onClick={handleClick}>Click Me</button>
</p>
<p>You typed: {state.username}</p>
<p>
<input onChange={handleChange} />
</p>
</div>
);
}
// This is generally not how you handle state
function setState(newState) {
Object.assign(state, newState);
renderApp();
}
function renderApp() {
ReactDOM.render(<App />, document.getElementById('root'));
}
renderApp();
</script>
</body>