Here's the scenario: a user must upload a file, once he does it, I would want to display it on the page as soon as the server receives it. I tried conditional rendering but that does not work.
What should I do to make it work? Thanks in advance.
code:
import React, { useState } from 'react'
import './App.css'
function App() {
const [image, setImage] = useState('')
const submitHandle = (e) => {
if (!image) {
console.log('please upload an image')
} else {
console.log(e.target)
e.preventDefault()
console.log('submitted')
}
}
return (
<section>
<div>
<div>
<h1>heading one</h1>
<form onSubmit={submitHandle}>
<input
value={image}
onChange={(e) => setImage(e.target.value)}
type='file'
accept='image/gif, image/jpeg, image/png'
/>
<button type='submit'>submit</button>
</form>
{image && <img src={image} alt='image' />}
</div>
</div>
</section>
)
}
export default App