I want to change the background image of a site based on the input the user types in. For example, if the user types in New York or New York City or NYC, the background image changes to a picture of NYC.
Right now, I have a div tag with a class and in a CSS file, the background image is set to a url from the internet. I've figured out that you need to import the file that you want to use as a background image if you want to set the inline style in the .jsx file but I think this would be a tedious method since I have hundreds of images that I want to render based on the input. So without using jQuery (I saw another post similar to this question but in jQuery and I've read that using jQuery with React is not a great idea and I'm not even sure I could make it work with my React setup) how would you change the background image of a div depending on an input value?
function App() {
const [name, setName] = useState('');
const [headingText, setHeadingText] = useState('City');
function handleChange(event){
setName(event.target.value);
}
function handleKeyDown(event){
if(event.key === 'Enter'){
setHeadingText(name);
}
}
return (
<div className="App">
<header className="App-header">
<input value={name} onKeyDown={handleKeyDown} onChange={handleChange} type="search" placeholder="Search for a city"></input>
</header>
</div>
);
}