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

147
Visualizações
How to get Tag component to know that it is wrapped under Tag Group component without using props?

So I have two components: Tag and TagGroup. When Tag component is used alone, I want that component to use <div> tag and when Tag is wrapped inside of TagGroup, which means more than 1 Tag is being used, then I want it to use <li> tag. With my current design, I used context to pass prop groupUpTags to Tag component. When groupUpTags is true then Tag component will use <li> tag, if false, <div> will be used. Currently, it doesn't even work properly as whether I wrap Tag component under TagGroup or not <li> tag is always being used. My question is, is there a way to get Tag component to render <li> tag when wrapped inside TagGroup without using any props and render <div> tag when used alone? My current design look little too messy. https://codesandbox.io/s/tag-group-s07oyh?file=/src/App.tsx

App.tsx

import "./styles.css";
import React from "react";
import { TagGroupContextInterface, TagGroupContext } from "./TagGroupContext";

export interface TagGroupInterface extends TagGroupContextInterface {
  children: React.ReactNode;
}

const Tag = () => {
  const context = React.useContext(TagGroupContext);
  return context.groupUpTags ? (
    <li className="tag">Tag</li>
  ) : (
    <div className="tag">Tag</div>
  );
};

const TagGroup = ({ groupUpTags = true, children }: TagGroupInterface) => {
  const context = React.useMemo(
    () => ({
      groupUpTags
    }),
    [groupUpTags]
  );

  return (
    <TagGroupContext.Provider value={context}>
      <div>
        <ul>{React.Children.map(children, (child) => child)}</ul>
      </div>
    </TagGroupContext.Provider>
  );
};

export default function App() {
  return (
    <div className="App">
      <TagGroup>
        <Tag />
        <Tag />
        <Tag />
      </TagGroup>
    </div>
  );
}

TagGroupContext.tsx

import React from "react";

export interface TagGroupContextInterface {
  groupUpTags?: boolean;
}

const defaultTagGroupContext: TagGroupContextInterface = {
  groupUpTags: true
};

export const TagGroupContext = React.createContext<TagGroupContextInterface>(
  defaultTagGroupContext
);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Keep your components more deterministic

A Tag component should probably always return a simple tag in a div or a span whatever you need for the stand alone tag to work.

I would then add the li inside the list component since thats the moment you need the li. when you code a Tag it shouldn't know about the parents.

Probably something like this in your Tag Group

{Children.map(children, (child, i) => {
  <li key={i}>{child}</li>;
})}

note: don't use "i" as a key its not reliable. Use a uid from the tag itself.

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