I have this assignment and I am almost done with it. Just stuck with this issue where I want to clear the input text field after submitting the values. Below is my code :
index.js
import react from 'react';
import reactdom from 'react-dom';
import App from './App';
reactdom.render(<App></App>, document.getElementById("root"));
App.js
import react, { useState } from 'react';
function App(){
const [txt, setTxt] = useState("");
const [msg, setMsg] = useState("");
function changeTxt(event){
setTxt(event.target.value);
}
function displayMsg(event){
setMsg(txt);
event.preventDefault();
}
return (
<div className='container'>
<form>
<h1>Hello {msg}</h1>
<input onChange={changeTxt} type="text" placeholder="Enter your text here.." ></input>
<button onClick={displayMsg} id="submitBtn">Submit</button>
</form>
</div>
);
}
export default App;
And these are the screenshots of the outputs :
** Fig 1 : Initial Output that I have**
So your elements are getting transformed by react to “React.createElement” so “React” capitalised has to be in scope.
If you imported “react” from “react” then your import is not getting used and something else is guarding this bug from happening. (JavaScript is case sensitive, just for the sake of experimentation remove the import completely, does it still work? )
I suspect that the environment and the version of the library you are using, prevent an issue in your end but that does not mean that import “react” from “react” is not wrong.
If you end up to work in another environment or in a legacy app this issue may end up to bite you.