[英]What is the type of the useState setter function?
有時必須傳遞useState
掛鈎的 setter 函數。 鍵入該函數的正確方法是什么?
// some component with useState
const [infoText, setInfoText] = useState<string>("");
// same component calls a function in which the setter function is needed.
getInfoText(setInfoText);
// in the definition of that function (getInfoText) I now want to correctly type the setter function
export function getInfoText(setInfoText: ???) {
PS:我知道有幾個類似的問題。 我想提供一個沒有上下文的一般問題,例如:這段代碼的正確類型是什么。
狀態設置器的類型是這樣的:
React.Dispatch<React.SetStateAction<TypeOfTheState>>
對於這個:
const [infoText, setInfoText] = useState<string>("");
setInfoText
的類型為:
React.Dispatch<React.SetStateAction<string>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.