Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
how to pass useState set state function to a child component in typescript?

I recently started using typescript and I'm having trouble with passing a use state setter function,

my parent component is is this :

const [word, setword] = useState('run')

and in the child im passing :

<Child word={word} setword={setword}/>

the child has this :

    interface Props {
      word: string
      
      setword: React.Dispatch<React.SetStateAction<string>>;
    }
    const VoicetoText: React.FC<Props> = (word, setword) => {
    return (
{word}
<button onClick={()=>setword('hey buddy')} ><button/>
    )}

i keep getting an error :enter image description here

about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

You forgot to add a curly brace to

const VoicetoText: React.FC<Props> = (word, setword) 

either use props or deconstruct the props

const VoicetoText: React.FC<Props> = (props) 
props.setWord

deconstruct

const VoicetoText: React.FC<Props> = ({word, setword}) 
about 4 years ago · Santiago Gelvez Denunciar

0

I would suggest to not make your life harder and go wtih something like:

const Child = ({setWord}: {setWord: (arg0: string}) => void) => {
  // () => setWord("word") is jsut for sample sake, you should avoid using arrow function calls inside event handleres like this
  return <button onClick={() => setWord("word")}>Click me</button>
}

const Parent = () => {

  const [word, setWord] = useState(false);

  return <Child propName={setWord}/>
}
about 4 years ago · Santiago Gelvez Denunciar

0

Just use setword: (word: string) => void as the prop type.

interface Props {
  word: string      
  setword: (word: string) => void;
}
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda