Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 与TypeScript';这是严格的初始化_Reactjs_Typescript_React Context - Fatal编程技术网

Reactjs 与TypeScript';这是严格的初始化

Reactjs 与TypeScript';这是严格的初始化,reactjs,typescript,react-context,Reactjs,Typescript,React Context,我正在尝试转换已经运行的React应用程序,所以它将遵守TypeScript严格的规则,并且有点被上下文API卡住了。一般使用方法如下: // context type and producer are defined in one place for the whole project import GlobalContext from '../context/GlobalContext'; class MyComponent extends React.Component { pu

我正在尝试转换已经运行的React应用程序,所以它将遵守TypeScript严格的规则,并且有点被上下文API卡住了。一般使用方法如下:

// context type and producer are defined in one place for the whole project
import GlobalContext from '../context/GlobalContext';

class MyComponent extends React.Component {
    public context: React.ContextType<typeof GlobalContext>;
    // other component logic
}
MyComponent.contextType = GlobalContext;

但是还有更惯用的解决方案吗?

钩子之所以伟大的另一个原因。钩子之所以伟大的另一个原因。。
public context!: React.ContextType<typeof GlobalContext>;