This React app takes Text inputs (AddMovie.js) and displays them on another page (MovieList.js).
Input one: Movie name & Input two: Price
Getting strings to display was fairly straight forward, but now I want to take in an Image URL in a third text input and display the image along with the name and price.
I'm new to React and I'm not quite sure where to modify. Any help would be massively appreciated.
Code for AddMovie. I'm not sure if everything needs to be handled under updateImage()?
const AddMovie = () => {
const [name, setName] = useState('');
const [price, setPrice] = useState('');
const [image, setImage] = useState('');
const [movies, setMovies] = useContext(MovieContext);
const updateName = (e) => {
setName(e.target.value);
};
const updatePrice = (e) => {
setPrice(e.target.value);
};
const updateImage = (e) => {
setImage(e.target.value);
};
const addMovie = (e) => {
e.preventDefault();
setMovies((prevMovies) => [
...prevMovies,
{ name: name, price: price, image: image },
]);
};
return (
<form onSubmit={addMovie}>
Enter Product Title
<br />
<input type='text' name={name} onChange={updateName} />
<br />
Enter Product Price
<br />
<input type='text' price={price} onChange={updatePrice} />
<br />
Enter Image Address
<br />
<input id='image' type='text' image={image} onChange={updateImage} />
<br />
<br />
<button>Submit</button>
</form>
);
};
Each individual Movie item. Not sure if {image} is correct.
const Movie = ({ name, price, image }) => {
return (
<div>
<h2>{name}</h2>
<h4>{price}</h4>
<image>{image}</image>
</div>
);
};
export default Movie;
This is for the List of movies displaying after they are added.
const MovieList = () => {
const [movie, setMovie] = useContext(MovieContext);
return (
<div>
{movie.map((movie) => (
<Movie name={movie.name} price={movie.price} image={movie.image} />
))}
</div>
);
};
export default MovieList;
image is not a native html element, and so you should modify your Movie component to
const Movie = ({ name, price, image }) => {
return (
<div>
<h2>{name}</h2>
<h4>{price}</h4>
<img src={image} />
</div>
);
};
export default Movie;