Load JSON into a context provider?
Answered
Gecco posted this in #help-forum
GeccoOP
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? ThanksAnswered by joulev
importing json files directly
in ts you can simply do
in js you probably need import assertions/attributes, look "import json to js" up for more info
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
2 Replies
@Gecco 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
importing json files directly
in ts you can simply do
in js you probably need import assertions/attributes, look "import json to js" up for more info
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
Answer
GeccoOP
Oh thanks so much, I've been looking for a solution to this issue and its been a real pain