【发布时间】:2020-04-13 08:58:06
【问题描述】:
我目前在当前设置中看到此错误。
输入 '({ team: string | null; } | { team: string | null; } | { ...; } | { ...; } | { ...; } | Dispatch<...>)[]' 缺少“状态”类型的以下属性:teamts(2739) index.d.ts(290, 9):预期类型来自属性“值”,该属性在此处在类型“IntrinsicAttributes & ProviderProps”上声明
我的代码如下,如果需要更多详细信息,请告诉我。
actions.ts
export const setTeam = (team: string | null) => ({
type: 'SET_TEAM',
team,
});
reducer.ts
export const initialState = {
team: null,
};
type State = {
team: string | null;
};
export const GlobalContext = React.createContext<State | null>(null);
export const reducer = (state: State, action: any) => {
switch (action.type) {
case actionTypes.SET_TEAM:
const team = action.team;
return {
...state,
team,
};
default:
return state;
}
};
App.tsx
const App = () => {
const [state, dispatch] = React.useReducer(reducer, initialState);
return (
// error with state and dispatch here
<GlobalContext.Provider value={[state, dispatch]}>
...App code
</GlobalContext.Provider>
)
}
Team.tsx
import { GlobalContext } from './reducers';
import { setTeam } from './actions';
const Team = () => {
const [, dispatch] = React.useContext(GlobalContext);
return <span onClick={() => dispatch(setTeam('LFC'))}>LFC</span>
}
【问题讨论】:
-
我不知道在哪里使用
setTeam()或者为什么这在这种情况下很重要...... -
您正在将上下文作为具有
team属性的对象键入,但您正在传递具有状态的数组并作为元素调度 -
@PatrickRoberts 更新了问题
-
@AsafAviv 那么你能解释一下我应该如何输入这个吗?抱歉,我不确定
标签: reactjs typescript ecmascript-6 react-hooks