A veces, la función de establecimiento de un useState debe pasarse. ¿Cuál es la forma correcta de escribir esa función?
// 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: ???) {PD: Sé que hay un par de preguntas similares. Quiero proporcionar una pregunta general sin contexto como: ¿cuál es el tipo correcto para este fragmento de código?
El tipo de setter del estado es así:
React.Dispatch<React.SetStateAction<TypeOfTheState>>Para este:
const [infoText, setInfoText] = useState<string>(""); setInfoText es de tipo:
React.Dispatch<React.SetStateAction<string>>