1

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;
1
  • 1
    Perhaps you're looking for the <img> element. Commented May 27, 2020 at 19:05

3 Answers 3

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect. Thank you!
@Akainoto if it worked for u, then accepting it as the correct answer will help others find it easier
0

Assuming movie.image contains an URL

const Movie = ({ name, price, image }) => {
  return (
    <div>
      <h2>{name}</h2>
      <h4>{price}</h4>
      <img src={image} alt={name}>
    </div>
  );
};

export default Movie;

That must do the job ;)

Comments

0

Below should work for you. As a suggestion, I would suggest you to rename image as imageUrl.

const Movie = ({ name, price, image }) => {
  return (
    <div>
      <h2>{name}</h2>
      <h4>{price}</h4>
      <img src={image} alt={name} />
    </div>
  );
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.