There's a couple of problems with this snippet:

{videos.id.map((video) => {
  <div key={video.id} {...videos}></div>
})}
  1. In your code the videos variable is initially null, and then (after fetching) — an array. Arrays don't have id property, and accessing properties of null is an illegal operation. You might want to first ensure that this is an array, rather than null, and then also remove the ".id" part.

  2. You don't return anything from .map()! Yes, you create a JSX element for each item of the array, but they are destroyed unused after that.

Consider using this instead:

{videos && videos.map((video) => (
  <div key={video.id} {...videos}></div>
))}

Another problem is with variables' visibility:

import videos from "./path/to/data.json";
// and then later
const [ videos, setVideos ] = useState(null);

That first variable videos (the imported one) is not visible anymore, it got shadowed by the second one.

You either rename the second variable to prevent shadowing, or remove the first one completely as it is unused.


Next thing that I can see is that the code can't make up its mind about what it is actually trying to accomplish. On one hand, router provides an ID to a specific, particular video, which means that we're trying to show only one video. On the other hand though, the FeaturedVideo component is almost a perfect fit for showing the list of all the videos.

Judging from the names and overall setup though, it is somewhat clear that you're trying to show one video, not the whole list.

Looks like you're using react-router. If that's true, in FeaturedVideo you need to access the video ID, provided by router. Given that it is v5+, you can use useParams hook for that:

import React, { useState, useEffect } from 'react';
import { useParams } from 'react-router';
const [ video, setVideo ] = useState(null); // 'video', not 'videos'
const { videoID } = useParams();

useEffect(() => {
  fetch("/src/data/data.json")
    .then((res) => res.json())
    .then((videos) => videos.filter((video) => {
      return video.id === videoID;
    }))
    .then((matched) => setVideo(matched[0]));
}, []);

// Here `video` variable holds either `null` or the first matched array item.
// Render it using suggestions above.

References:

  • .map() method of arrays
  • && operator (see Description section)
  • Variable shadowing
  • React Router Hooks
Answer from Parzh on Stack Overflow
Top answer
1 of 2
2

There's a couple of problems with this snippet:

{videos.id.map((video) => {
  <div key={video.id} {...videos}></div>
})}
  1. In your code the videos variable is initially null, and then (after fetching) — an array. Arrays don't have id property, and accessing properties of null is an illegal operation. You might want to first ensure that this is an array, rather than null, and then also remove the ".id" part.

  2. You don't return anything from .map()! Yes, you create a JSX element for each item of the array, but they are destroyed unused after that.

Consider using this instead:

{videos && videos.map((video) => (
  <div key={video.id} {...videos}></div>
))}

Another problem is with variables' visibility:

import videos from "./path/to/data.json";
// and then later
const [ videos, setVideos ] = useState(null);

That first variable videos (the imported one) is not visible anymore, it got shadowed by the second one.

You either rename the second variable to prevent shadowing, or remove the first one completely as it is unused.


Next thing that I can see is that the code can't make up its mind about what it is actually trying to accomplish. On one hand, router provides an ID to a specific, particular video, which means that we're trying to show only one video. On the other hand though, the FeaturedVideo component is almost a perfect fit for showing the list of all the videos.

Judging from the names and overall setup though, it is somewhat clear that you're trying to show one video, not the whole list.

Looks like you're using react-router. If that's true, in FeaturedVideo you need to access the video ID, provided by router. Given that it is v5+, you can use useParams hook for that:

import React, { useState, useEffect } from 'react';
import { useParams } from 'react-router';
const [ video, setVideo ] = useState(null); // 'video', not 'videos'
const { videoID } = useParams();

useEffect(() => {
  fetch("/src/data/data.json")
    .then((res) => res.json())
    .then((videos) => videos.filter((video) => {
      return video.id === videoID;
    }))
    .then((matched) => setVideo(matched[0]));
}, []);

// Here `video` variable holds either `null` or the first matched array item.
// Render it using suggestions above.

References:

  • .map() method of arrays
  • && operator (see Description section)
  • Variable shadowing
  • React Router Hooks
2 of 2
1

You are correct in using that useEffect hook. You want this data to load on component mount.

Assuming that URL returns JSON you can simply:

fetch(url)
  .then(res => res.json().then(videos => setVideos(videos)));
🌐
DEV Community
dev.to › antdp425 › react-fetch-data-from-api-with-useeffect-27le
React: Fetch Data from API with useEffect - DEV Community
March 14, 2021 - // 1. Import *useState* and *useEffect* import React, {useState, useEffect} from 'react'; import './App.css'; function App() { // 2. Create our *dogImage* variable as well as the *setDogImage* function via useState // We're setting the default value of dogImage to null, so that while we wait for the // fetch to complete, we dont attempt to render the image let [dogImage, setDogImage] = useState(null) // 3. Create out useEffect function useEffect(() => { fetch("https://dog.ceo/api/breeds/image/random") .then(response => response.json()) // 4.
Discussions

reactjs - useEffect and useState to fetch API data - Stack Overflow
I want to use useEffect(on mount) to fetch from API and store it in useState. Fetch API is used to get the data. The problem is when initial page loading and also when I reload the page, it outputs... More on stackoverflow.com
🌐 stackoverflow.com
reactjs - how to use react fetch() with useEffect hook and map the fetched data - Stack Overflow
function SlugBook() { // let {slug} = useParams(), const [state, setState] = useState([]) useEffect(() => { fetch("http://127.0.0.1:8000/app/reviews/",{CSRF_TOKEN....} ) .then(response => response.json()) .then(data => console.log(data)) -->works fine .then(data => setState(data)); --> not ... More on stackoverflow.com
🌐 stackoverflow.com
Setting React state from JSON data in useEffect
I need to fetch json data and iterate over it, fetching some more json data later, and finally setting React states. I'd like to use a general function to handle all file requests. I'd also like to handle different types of errors (404 and json parsing, mostly). I've read How to use async functions in useEffect ... More on stackoverflow.com
🌐 stackoverflow.com
Can't fetch local json using typescript "useEffect" in my RN Project - Stack Overflow
When you require() a local .json file, what I would expect to happen is to get an object instead of an URL. Fetch would then stingify that object and make a request to '[object Object]', which would naturally fail. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Pluralsight
pluralsight.com › tech insights & how-to guides › tech guides & tutorials
Fetch Data From a JSON File in a React App | Pluralsight
March 27, 2025 - If your JSON returns an object, simply check your state to be not null at the time of outputting it, otherwise you might get an error. Have a look at the entire code below. import React,{useState,useEffect} from 'react'; import './App.css'; function App() { const [data,setData]=useState([]); const getData=()=>{ fetch('data.json' ,{ headers : { 'Content-Type': 'application/json', 'Accept': 'application/json' } } ) .then(function(response){ console.log(response) return response.json(); }) .then(function(myJson) { console.log(myJson); setData(myJson) }); } useEffect(()=>{ getData() },[]) return ( <div className="App"> { data && data.length>0 && data.map((item)=><p>{item.about}</p>) } </div> ); } export default App;
🌐
Hashnode
marrnuel.hashnode.dev › simplify-your-data-handling-learn-to-load-json-files-into-react-with-useeffect-usestate-and-fetch-api
"Master Data Loading in React with JSON Using Hooks and Fetch API"
March 9, 2023 - Adding to the above code block, you import the useEffect hook which takes care of the data fetching from the JSON file and updating the state of the data from an empty array to an array containing the data.
🌐
Design+Code
designcode.io › react-hooks-handbook-fetch-data-from-an-api
Fetch Data from an API - React Hooks Handbook - Design+Code
However, sometimes, the website you want to fetch data from doesn't come with an already built package, but with a JSON API. You'll need to fetch data using the fetch function and the useEffect hook.
🌐
GeeksforGeeks
geeksforgeeks.org › fetching-data-from-an-api-with-useeffect-and-usestate-hook
Fetching Data from an API with useEffect and useState Hook | GeeksforGeeks
April 28, 2025 - Within the useEffect hook, a fetch request is made to the Random Data API endpoint that provides a list of random users. We use the query parameter size=5 to specify that we want to fetch 5 random users.
🌐
SheCodes
shecodes.io › athena › 52759-explaining-useeffect-and-fetch-in-react
[React] - Explaining useEffect and Fetch in React - | SheCodes
Learn about how to use the `useEffect` hook and the `fetch` function for making HTTP requests in React with this simple example.
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-call-web-apis-with-the-useeffect-hook-in-react
How To Call Web APIs with the useEffect Hook in React | DigitalOcean
October 6, 2020 - In this tutorial, you’ll use the useEffect and useState Hooks to fetch and display information in a sample application, using JSON server as a local API for testing purposes. You’ll load information when a component first mounts and save customer inputs with an API.
🌐
Medium
medium.com › @cwlsn › how-to-fetch-data-with-react-hooks-in-a-minute-e0f9a15a44d6
How to Fetch Data with React Hooks in a Minute | by Connor Wilson | Medium
February 8, 2019 - // hooks.js import { useState, useEffect } from "react";function useFetch(url) { const [data, setData] = useState([]); const [loading, setLoading] = useState(true); async function fetchUrl() { const response = await fetch(url); const json = await response.json(); setData(json); setLoading(false); } useEffect(() => { fetchUrl(); }, []); return [data, loading]; } export { useFetch };
🌐
Newline
newline.co › @RichardBray › understanding-reacts-useeffect-for-fetching-api-data-with-promises--7706d232
Understanding React's useEffect for Fetching API Data with Promises
January 17, 2025 - In React development, one of the challenges many developers encounter is managing side effects, particularly when fetching API data. The useEffect hook plays a pivotal role here, allowing us to synchronize our component's lifecycle with external data. In this post, we'll illuminate how to ...
🌐
React
react.dev › reference › react › useEffect
useEffect – React
If you want to fetch data from an Effect manually, your code might look like this: ... Note the ignore variable which is initialized to false, and is set to true during cleanup. This ensures your code doesn’t suffer from “race conditions”: network responses may arrive in a different order than you sent them. ... import { useState, useEffect } from 'react'; import { fetchBio } from './api.js'; export default function Page() { const [person, setPerson] = useState('Alice'); const [bio, setBio] = useState(null); useEffect(() => { let ignore = false; setBio(null); fetchBio(person).then(result
🌐
Medium
arkumari2000.medium.com › fetch-data-from-a-local-json-file-in-react-js-in-functional-component-be1a4b66ebd1
Fetch Data from a local JSON file in react js (in functional component) | by Archana Kumari | Medium
May 31, 2021 - utils/get-data.js export const getData = async (filePath, fileType) => { try{ const response = await fetch(filePath); switch (fileType.toUpperCase()) { case 'JSON': return response.json(); default: return response; } } catch (error) { return error; } } We can give filePath and fileType a value like this: filePath="path of json file", fileType="file type here" ... hooks/customHook.js import { useState, useEffect } from 'react'; import { getData } from '../utils';export function customHook(){ const [data, setData] = useState(); useEffect(()=>{ fetchData(); },[]) const fetchData = async () => { try { const response = await getData('data.json', 'json'); setData(response.persons); } catch (error) { console.error(error); } }; return(data); }
🌐
Max Rozen
maxrozen.com › fetching-data-react-with-useeffect
Fetching Data in React with useEffect - Max Rozen
Fetching data from an API, communicating with a database, and sending logs to a logging service are all considered side-effects, as it's possible to have a different output for the same input. For example, your request might fail, your database might be unreachable, or your logging service might have reached its quota. This is why useEffect is the hook for us - by fetching data, we're making our React component impure, and useEffect provides us a safe place to write impure code.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-fetch-api-data-in-react
How to Fetch API Data in React
December 14, 2023 - import { useState, useEffect } from 'react'; const Fetch = () => { const [photos, setPhotos] = useState([]); useEffect(() => { fetch('https://jsonplaceholder.typicode.com/photos') .then((res) => { return res.json(); }) .then((data) => { console.log(data); setPhotos(data); }); }, []); return ( <div> {photos.map((photo) => ( <img key={photo.id} src={photo.url} alt={photo.title} width={100} /> ))} </div> ); }; export default Fetch;
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs › fetching-data-from-an-api-with-useeffect-and-usestate-hook
Fetching Data Using useEffect and useState Hook - GeeksforGeeks
August 12, 2025 - Within the useEffect hook, we perform a fetch request to the Random Data API endpoint that provides random user data. Upon receiving a response, we convert it to JSON format and update the userData state with the fetched data using the setUserData ...