Next.js useContext issue

907 Views Asked by At

I'm trying to provide my data via ContextProvider to my own reactComponent. I've create Context.jsx (I wanted to have external context file). But when I try to connect my Context.jsx with _app.jsx I have an arror:

Could not find a declaration file for module './Context.jsx'. 'Context.jsx' implicitly has an 'any' type.ts(7016)

enter image description here

And here below the code of my Context.jsx:

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

const Context = createContext();

const Provider = ({ children }) => {
    // the value that will be given to the context
    const [code, setCode] = useState(null);

    useEffect(() => {
        const fetchBlogs = () => {
            fetch(`https://node-test-mongo.herokuapp.com/api/blog`)

                .then((response) => {
                    return response.json();
                })
                .then((data) => {
                    setCode(data.blogs)
                })
                .catch((error) => console.log("An error occured"));
        };

        fetchBlogs();
    }, []);

    // the Provider gives access to the context to its children
    return <Context.Provider value={code}>{children}</Context.Provider>;
};

export const useCoder = () => useContext(Context);

export default Provider;

What the issue could be here?

Thank you in advance for help:)

0

There are 0 best solutions below