VictorBlanco
VictorBlanco

Reputation: 9

React. Two buttons in a component but only one button works

I am practicing React basics. I have a component mapped from an array of objects and it shows twice in my website but only on the second time the button works. the first button does not seem to be recognised.

Can someone explain why please? I can't find a reason for this but I am sure it is something obvious I am missing

A link to sandbox

App.js

import MainComponent from "./components/MainComponent";

function App() {
  return (
    <div>
      <MainComponent />
    </div>
  );
}

export default App;

MainComponent.js

import React from "react";
import TextItem from "./TextItem";

const text = [
  {
    image:
      "https://pbs.twimg.com/profile_images/626298192418607105/4KBKHQWi_400x400.jpg",
    title: "This is a whale",
    subtitle: "It is a large mammal",
    price: "£ 167.87 + VAT",
    uom: "per Unit",
  },
  {
    image:
      "https://news.artnet.com/app/news-upload/2019/01/Cat-Photog-Feat-256x256.jpg",
    title: "This is a cat",
    subtitle: "It is a small mammal",
    price: "£ 17.87 + VAT",
    uom: "per Unit",
  },
];

const MainComponent = () => {
  return (
    <div>
      {text.map((eachText, i) => (
        <TextItem
          key={i}
          image={eachText.image}
          title={eachText.title}
          subtitle={eachText.subtitle}
          price={eachText.price}
          uom={eachText.uom}
        />
      ))}
    </div>
  );
};

export default MainComponent;

TextItem.js

import React from "react";
import styled from "styled-components";
import Card from "../UI/Card";

const TextItem = (props) => {
  const onClickHandler = () => {
    console.log("clicked");
  };

  return (
    <Card>
      <DivFlex>
        <ImgDiv>
          <img src={props.image} alt={"a whale"} />
        </ImgDiv>
        <TitleDiv>
          <h1>{props.title}</h1>
          <h3>{props.subtitle}</h3>
        </TitleDiv>
        <PriceDiv>
          <h2>{props.price}</h2>
          <h2>{props.uom}</h2>
        </PriceDiv>
        <EditDiv>
          <Button onClick={onClickHandler}>Edit this</Button>
        </EditDiv>
      </DivFlex>
    </Card>
  );
};

const DivFlex = styled.div`
  display: flex;
  justify-content: center;
  align-items: center;
  width: 800px;
  height: 600px;
`;

const ImgDiv = styled.div`
  width: 90px;
  height: 90px;
  margin-right: 3%;

  img {
    max-width: 100%;
    height: 100%;
    border-radius: 6px;
  }
`;

const TitleDiv = styled.div`
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
  width: 30%;
  margin-right: 3%;
`;
const PriceDiv = styled.div`
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-end;
  width: 20%;
  margin-right: 3%;
`;
const EditDiv = styled.div`
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: auto;
  margin-left: 3%;
`;
const Button = styled.div`
  outline: none;
  border: none;
  background: maroon;
  color: white;
  padding: 1.2rem 1.8rem;
  font-size: 22px;
  border-radius: 8px;
  cursor: pointer;
`;

export default TextItem;

Upvotes: 0

Views: 481

Answers (1)

vighnesh153
vighnesh153

Reputation: 5386

This is happening due to the conflicting heights of Card and DivFlex. The Card has an height of 140px but its child DivFlex has a height of 600px. So, the next item is basically overlapping the previous item and hence you are not able to click the first button. You can observe the same behaviour if you had any number of items. Only the last item would be clickable due to it overlapping on top of previous item.

One way to fix this is to remove the height from Card component and change the height of DivFlex to 140px instead.

PS: It didn't happen in the replica sandbox because you didn't use the Card component there and used a regular div which by default has a height of auto

Upvotes: 2

Related Questions