Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the exact functionality of Search (Data.Search) function in the Code is it built-in funnction to search in Json?

what is Data.search here

import React, { useState, useEffect } from "react";

import MovieCard from "./MovieCard";

import SearchIcon from "./search.svg";

import "./App.css";

const API_URL = "http://www.omdbapi.com?apikey=b6003d8a";

const App = () => {

  const [searchTerm, setSearchTerm] = useState("");

  const [movies, setMovies] = useState([]);

  useEffect(() => {

    searchMovies("Batman");

  }, []);

  const searchMovies = async (title) => {

    const response = await fetch(`${API_URL}&s=${title}`);

    const data = await response.json();
    
    setMovies(data.Search);

    
  };


why don’t we write simply setMovies(data);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

no, it is not. you are fetching data from an api and it returns an object, which has a value called search, therefore you are setting data.search value as your movies, not entire object returned from the api, you can check this easily by logging both fields, like so :

const data = await response.json();
   console.log('this is data' , data)
   console.log('this is data.search', data.search)
setMovies(data.Search);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading