'how to fetch data from json file using vite reactjs

Note

vitejs build tool used to create reactjs-app.

Question

I can read data.json in development server but i can't read data.json from production how can i fix this.

File Tree

code for fetching data.json

    fetch('data/data.json').then(res => res.json()).then(d => {
        setNet({...net, isLoading: !net.isLoading})
        setData(d);
    }).catch(err => setNet({...net, msg: <p>Some thing went <span className="err__wrong">wrong</span> please try again after some time....</p>}))
  }, [])

if i build reactjs using npm run build in dist file there is no data.json file.

anyone can fix this?

Here is the project repo



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source