React said: "Can not find namespace..." in creation of Context
Asked Answered
S

1

9

I create a Context in React.js with TypeScript but this error had occurred: Cannot find namespace 'JobContext'.ts(2503)

import React, { createContext, useState }       from 'react';

export const JobContext = createContext<any>({});

const JobContextProvider = ({ children }) => {

    const [data, setData] = useState<any>();

    const updatingState = (newData) => setData({ ...data, ...newData });

    return (
        <JobContext.Provider value={{data, updatingState}}>
            {children}
        </JobContext.Provider>
    );

};

export default JobContextProvider;
Sectional answered 9/9, 2020 at 12:22 Comment(0)
S
30

Because as React has said, the extension of your file is .ts, which should be .tsx.

Sectional answered 16/9, 2020 at 15:3 Comment(2)
Hours and hours again to find this solution Many thanksGaulin
OH MY GOD. this save me.. really save meFloatfeed

© 2022 - 2024 — McMap. All rights reserved.