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

66
Visualizações
Convert dynamic class by state value from react to typescript

I need to use this react function, but in typescript

const ListItem = ({ text }) => {
    let [showMore, setShowMore] = useState(false);

    return (
        <div className="item">
            <div>
                <div className={`text ${showMore ? "active" : ""}`}>{text}</div>
            </div>
            <button onClick={() => setShowMore((s) => !s)}>Show more</button>
        </div>
    );
};

I have tried something like this, but i am getting some errors

<html>TS2345: Argument of type '(s: showContent) =&gt; boolean' is not assignable to parameter of type 'SetStateAction&lt;showContent&gt;'.<br/>Type '(s: showContent) =&gt; boolean' is not assignable to type '(prevState: showContent) =&gt; showContent'.<br/>Type 'boolean' is not assignable to type 'showContent'.
interface showContent {
  state: boolean;
}

let [showMore, setShowMore] = React.useState<showContent>({ state: false });

    return (
        <div className="item">
            <div>
                <div className={`text ${showMore ? "active" : ""}`}>{text}</div>
            </div>
            <button onClick={() => setShowMore((s) => !s)}>Show more</button>
        </div>
    );
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Conversion to typescript

If your intention was to convert the first block to typescript, then you do not need the showContent interface as you're only providing a boolean value.

import React, { ReactNode, useState } from "react";

interface Props {
  text: ReactNode;
}

const ListItem = ({ text }: Props) => {
    let [showMore, setShowMore] = useState(false);

    return (
        <div className="item">
            <div>
                <div className={`text ${showMore ? "active" : ""}`}>{text}</div>
            </div>
            <button onClick={() => setShowMore((s) => !s)}>Show more</button>
        </div>
    );
};

See this Typescript Playground

About the error

The error you were getting:

Argument of type '(s: showContent) => boolean' is not assignable to parameter of type 'SetStateAction'.
Type '(s: showContent) => boolean' is not assignable to type '(prevState: showContent) => showContent'.
Type 'boolean' is not assignable to type 'showContent'.(2345)

Is trying to tell you that you cannot assign a boolean (meaning true or false) to an object with signature { state: boolean; }. This is why @pilchard suggested to create an object to match your interface ({ state: !s.state }).

But as the code shows above, I don't think you need this object at all, it seems like a typing mistake.

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