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

173
Visualizações
Dynamically load and select custom item objects in React

I am dynamically creating a list from an API call on my backend.

data && data.map( business => BusCard(business) ) where data contains a giant list of businesses from an API call.

On my gui I want to be able to select an item to process it later in some other functions (change border color of selected item, get businesses reviews, ratings, etc...). I was only able to get the selection working, but I think the way I am doing it here is very hacky.

I need some pointers on how I should go about doing this.

Namely

  function setBusiness() 
  {
    currentBusiness = item;
  }

and the custom onClick I just call that. To set it. <Box ... onClick={setBusiness}>

I call this function whenever i need to get the item that was selected.

export function getCurrentSelectedBusiness()
{
  return currentBusiness;
}

enter image description here

My custom "BusCard" That shows the information from a business.

import { AspectRatio, Box, Image} from "@chakra-ui/react";
import React, {useState} from 'react';

let currentBusiness = "";

export function getCurrentSelectedBusiness()
{
  return currentBusiness;
}

function BusCard(item) 
{
  function setBusiness() 
  {
    currentBusiness = item;
  }

  return(
    <Box p="4" maxW="sm" borderWidth="1px" borderRadius="lg" overflow="hidden" mt={6}  onClick={setBusiness}>
      <AspectRatio maxH="100px">
        <Image src={item.image_url} alt={item.alias} size="sm" />
      </AspectRatio>
      <Box p="2">
        <Box
          mt="1"
          fontWeight="semibold"
          as="h4"
          lineHeight="tight"
          alignItems
        >
          {item.name} 
          <br />
          {item.review_count} Reviews
        </Box>
        <Box display="flex" alignItems="baseline">
          <Box
            color="gray.500"
            fontWeight="semibold"
            letterSpacing="wide"
            fontSize="xs"
            textTransform="uppercase"
            ml="5"
          >
            {item.location.display_address}
            <br />
            {item.phone}
          </Box>
        </Box>
      </Box>
    </Box>
  )
}


export default BusCard;
about 4 years ago · Juan Pablo Isaza
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