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

104
Visualizações
Removing a border on the last element in a list React

Hey guys I'm trying to remove the border of the last item in an unordered list when the list grows to a certain size. My initial thought was something like:

document.querySelector('.employee-list-item:last-child').style.border = "none";

However, React says it can't set the style property on 'null.' Is it trying to target the element before it's been rendered? Any workarounds for this?

Here's my code:

import "../css/Employee.css";
import Avatar from "./Avatar";
import React from "react";

const Employee = (props) => {
  // capitalize first letter of firstName and first letter of lastName
  const name = props.name
    .split(" ")
    .map((i) => i[0].toUpperCase() + i.slice(1))
    .join(" ");

  const { title } = props;

  return (
    <li className="employee-list-item">
      <Avatar name={props.name} />
      <span className={"employee-name"}>{name}</span>
      <span className={"employee-title"}>{title}</span>
    </li>
  );
};

export default Employee;
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Create a new file called "App.css" and insert some CSS code in it:

OR

You can add CSS - Employee.css

.employee-list-item:last-child{border:none !important;}

Import the stylesheet in your application:

import './App.css';
about 4 years ago · Juan Pablo Isaza Relatório

0

You should put the code into a useEffect hook - that ensures it will run after React has done all its magic and the Virtual DOM has been rendered to the live DOM. The the code you already thought of should work with a tweak:

useEffect(() => {
    document.querySelector('.employee-list-item:last-child')[0].style.border = "none"; // or forEach(... if you expect more than one item to match
});
about 4 years ago · Juan Pablo Isaza Relatório

0

One way is pass a prop (ex isLast) to Employee and style based on that.

const Employee = (props) => {
  return (
    <li
      className="employee-list-item"
      style={props.isLast ? { border: "none" } : undefined}
    ></li>
  );
};

const List = () => {
  return (
    <div>
      {employees.map((emp, index) => (
        <Employee isLast={index === employees.length - 1} />
      ))}
    </div>
  );
};
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