Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
Passing state as a prop in React router v6 with typescript

Please excuse my ignorance as I am new to React router v6 and Typescript. I am trying to pass a state and a function that will change the state down the data flow of my application.

const [playerSystem, setPlayerSystem] = useState<String>("Cheese")
      <UImaster />
    
      <Routes>
        <Route path="/sectora" element={<SectorView  {...playerSystem} /> } />
</Routes>

However when I do this if I put both playerSystem and setPlayerSystem into the props for the element it throws an error. And at the moment my SectorView component is throwing this incredibly unclear error.

Type '{ toString(): string; charAt(pos: number): string; charCodeAt(index: number): number; concat(...strings: string[]): string; indexOf(searchString: string, position?: number | undefined): number; ... 44 more ...; Symbol.iterator: IterableIterator<...>; }' has no properties in common with type 'IntrinsicAttributes'.


How can I pass state and state changing functions down to other components?
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

playerSystem is a string, so you cannot pass it as a prop by spreading it. The error message is listing all the properties on a string and complaining that they don't match IntrinsicAttributes, which is a confusing way of referring to the set of thing you can pass as props to an HTML element.

This is how you would pass those values as props:

<SectorView playerSystem={playerSystem} setPlayerSystem={setPlayerSystem} />

Spreading in general does work on strings, but in the case of props, I believe only objects will work because props are a set of keys and values. So, for example, this is equivalent to the above:

const [playerSystem, setPlayerSystem] = useState<String>("Cheese")
const props = { playerSystem, setPlayerSystem }

<SectorView {...props} />

Note that { playerSystem, setPlayerSystem } is shorthand for { playerSystem: playerSystem, setPlayerSystem: setPlayerSystem }.

I would guess that the reason it doesn't work at all when you try to include setPlayerSystem is you're doing {...setPlayerSystem} and you can't spread a function ever. With playerSystem it lets you do the spread because that's allowed on strings, but then SectorView has no idea what to do with it, which is why the error comes from there.

Here are examples of how spreading works (independent from JSX) with objects, strings, and arrays.

const obj1 = { a: 1 }
const obj2 = { b: 2 }

console.log({ ...obj1, ...obj2 }) // { a: 1, b: 2 }

const arr1 = [1, 2, 3]
const arr2 = [4, 5, 6]

console.log([...arr1, ...arr2]) // [1, 2, 3, 4, 5, 6]

const str1 = "ab"
const str2 = "cd"

console.log([ ...str1, ...str2]) // ["a", "b", "c", "d"]

about 4 years ago · Juan Pablo Isaza Relatório

0

Is the Props type defined in the <SectorView /> component?

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda