New to ReactJS with experience in android
I simply set a value to variable on input change and use it in display
But the input freezes and new value is also not applied
I also declared the var msg outside render function so it isn't initialized every time
This is certainly not how react works and i'm doing trivial mistake but it may be so trivial that there are no answers out there
What am i doing wrong
import React, { useState } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
var msg = "initial value";
const Message = () => {
return (
<div>
<input
type="text"
value={msg}
placeholder="Enter a message"
onChange={(e) => {
alert(e.target.value);
msg = e.target.value;
}}
/>
<p>
<strong>{msg}</strong>
</p>
</div>
);
};
const rootElement = document.getElementById("root");
ReactDOM.render(<Message />, rootElement);
Here's a link to live demo
https://codesandbox.io/s/usestate-01-forked-f2vdq?file=/src/index.js:0-543
useState? You even import it.