Next.js Discord

Discord Forum

Load JSON into a context provider?

Answered
Gecco posted this in #help-forum
Open in Discord
Hi there, Is there a way in Next to load a JSON file and place the contents into a context provider? I can't seem to find a good way due to interference of server and client components since useContext is a client side device while fs is solely available server side. What would be the sollution to this? Thanks
Answered by joulev
importing json files directly

in ts you can simply do import data from "./file.json"

in js you probably need import assertions/attributes, look "import json to js" up for more info
View full answer

2 Replies

Answer
Oh thanks so much, I've been looking for a solution to this issue and its been a real pain