Reactjs 与TypeScript';这是严格的初始化
我正在尝试转换已经运行的React应用程序,所以它将遵守TypeScript严格的规则,并且有点被上下文API卡住了。一般使用方法如下: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
// 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>;