Nextjs-Problem – Untergeordnete Eigenschaften sind für Typ {} nicht vorhanden
P粉658954914
2023-08-16 20:20:47
<p>Ich verwende React und Next.js. Unten ist mein Code. Er löst einen Fehler im Attribut <code>children</code> aus. Die Fehlermeldung lautet <strong>Propertychildren im Typ {}. existiert nicht</strong></p>
<pre class="brush:php;toolbar:false;">import { NextPage } from "next";
import { createContext, useContext, useReducer, Dispatch } from „react“;
import { GlobalStatesType } from "../types";
import Reducer, { ActionType, initialState } aus „../reducers“;
export const StateContext = createContext<{
Zustände: GlobalStatesType;
Versand: Dispatch<ActionType>;
}>({
Staaten: initialState,
Versand: () =>
});
export const StateProvider: NextPage = ({children }) =>
const [States, Dispatch] = useReducer(reducer, initialState);
zurückkehren (
<StateContext.Provider value={{ states, distribution }}>
{ Kinder }
</StateContext.Provider>
);
};
export const useStatesValue = () => useContext(StateContext);</pre>
<p>Wie schreibe ich den Code im Kontext der nächsten Funktion, die ich importiert habe? </p>
看起来你正在使用TypeScript和Next.js创建一个上下文提供者组件。你遇到的错误"属性'children'在类型'{}'上不存在",很可能是因为TypeScript在函数组件中无法识别children属性。
要解决这个问题,你可以在StateProvider组件中显式定义children属性的类型。以下是如何做到这一点:
通过定义StateProviderProps类型并使用它来指定StateProvider组件中children属性的类型,你将不再遇到与children属性相关的TypeScript错误。