我有一个带有状态提供程序的React Context文件,用于管理cookie首选项。一切都按预期工作,但我的Typescript有1个问题。我的文件如下:
import Cookies from 'js-cookie';
import React, { Component, ReactNode } from 'react';
interface Props {
children: ReactNode | ReactNode[];
functionalCookiesOn: boolean;
performanceCookiesOn: boolean;
};
interface State {
functionalCookiesOn: boolean;
performanceCookiesOn: boolean;
};
// Initialise context storage
const CookiesContext = React.createContext({});
const { Consumer: CookiesConsumer } = CookiesContext;
class CookiesProvider extends Component<Props, State> {
constructor(props: Props) {
super(props);
const {
functionalCookiesOn,
performanceCookiesOn,
} = this.props;
this.state = {
functionalCookiesOn,
performanceCookiesOn,
};
}
componentDidUpdate(_prevProps: Props, prevState: State) {
const {
functionalCookiesOn,
performanceCookiesOn,
} = this.state;
// Set cookie to store functional cookies setting
if (functionalCookiesOn !== prevState.functionalCookiesOn) {
Cookies.set('functionalCookiesOn', functionalCookiesOn.toString());
}
// Set cookie to store performance cookies setting
if (performanceCookiesOn !== prevState.performanceCookiesOn) {
Cookies.set('performanceCookiesOn', performanceCookiesOn.toString());
}
}
toggleAllCookies = () => {
// Store reversed state for functional and performance cookies
this.setState((prevState: State) => ({
functionalCookiesOn: !prevState.functionalCookiesOn,
performanceCookiesOn: !prevState.performanceCookiesOn,
}));
}
render() {
const { children } = this.props;
const {
functionalCookiesOn,
performanceCookiesOn,
} = this.state;
const value = {
functionalCookiesOn,
performanceCookiesOn,
toggleAllCookies: this.toggleAllCookies,
};
return (
<CookiesContext.Provider value={value}>
{children}
</CookiesContext.Provider>
);
}
}
export default CookiesContext;
export { CookiesConsumer, CookiesProvider };
当我在另一个功能组件中使用它时,它看起来像这样:
const AnotherComponent = () => {
const {
functionalCookiesOn,
performanceCookiesOn,
toggleAllCookies,
} = useContext(CookiesContext);
return (
...
);
}
这会引发错误,例如:
Property 'functionalCookiesOn' does not exist on type '{}'.
在我看来,这与原始文件中的以下行有关:
const CookiesContext = React.createContext({});
因为我用一个空对象初始化了上下文(因为在那个阶段它没有任何值)。
初始化此错误或应用类型以避免错误的正确方法是什么?
答案 0 :(得分:1)
我认为您可以为致电createContext
的电话提供一种类型
const CookiesContext = React.createContext<Partial<Props>>({});
<Partial>
允许您创建不带默认值的上下文。
答案 1 :(得分:0)
尝试一下;-
import Cookies from 'js-cookie';
import React, { Component, ReactNode } from 'react';
interface Props {
children: ReactNode | ReactNode[];
functionalCookiesOn: boolean;
performanceCookiesOn: boolean;
};
interface State {
functionalCookiesOn: boolean;
performanceCookiesOn: boolean;
};
// Initialise context storage
const CookiesContext = React.createContext({
functionalCookiesOn: false,
performanceCookiesOn: false,
toggleAllCookies: () => null
});
const { Consumer: CookiesConsumer } = CookiesContext;
class CookiesProvider extends Component<Props, State> {
constructor(props: Props) {
super(props);
const {
functionalCookiesOn,
performanceCookiesOn,
} = this.props;
this.state = {
functionalCookiesOn,
performanceCookiesOn,
};
}
componentDidUpdate(_prevProps: Props, prevState: State) {
const {
functionalCookiesOn,
performanceCookiesOn,
} = this.state;
// Set cookie to store functional cookies setting
if (functionalCookiesOn !== prevState.functionalCookiesOn) {
Cookies.set('functionalCookiesOn', functionalCookiesOn.toString());
}
// Set cookie to store performance cookies setting
if (performanceCookiesOn !== prevState.performanceCookiesOn) {
Cookies.set('performanceCookiesOn', performanceCookiesOn.toString());
}
}
toggleAllCookies = () => {
// Store reversed state for functional and performance cookies
this.setState((prevState: State) => ({
functionalCookiesOn: !prevState.functionalCookiesOn,
performanceCookiesOn: !prevState.performanceCookiesOn,
}));
}
render() {
const { children } = this.props;
const {
functionalCookiesOn,
performanceCookiesOn,
} = this.state;
const value = {
functionalCookiesOn,
performanceCookiesOn,
toggleAllCookies: this.toggleAllCookies,
};
return (
<CookiesContext.Provider value={value}>
{children}
</CookiesContext.Provider>
);
}
}
export default CookiesContext;
export { CookiesConsumer, CookiesProvider };
这应该可以解决,打字稿将开始智能感知。