Next.js Discord

Discord Forum

Onclick event is not triggered when it is in a child component

Answered
Jenn posted this in #help-forum
Open in Discord
Avatar
I have these functions that I declared in the parent component and I am using this:
 {data ? (
                      <CartComponent
                        cart={cart}
                        addQuantity={addQuantity}
                        reduceQuantity={reduceQuantity}
                        removeFromCart={removeFromCart}
                        waterTypes={waterTypes}
                        addToCart={addToCart}
                      />
                    ) : (
                      <p>No data</p>
                    )}

And the cart component: I can see the data but I cannot click on the buttons
interface CartComponentProps {
  //rest of the codes
  addQuantity: (itemId: string) => void;
  reduceQuantity: (itemId: string) => void;
  removeFromCart: (itemId: string) => void;
  addToCart: (waterType: WaterType) => void;
}

const CartComponent: React.FC<CartComponentProps> = ({
  cart,
  addQuantity,
  reduceQuantity,
  removeFromCart,
  addToCart,
}) => {
  return (
    <div>
      <h1>Water Available:</h1>
      {data.map((dataItem) => (
        <div key={dataItem.id}>
          <button onClick={() => addToCart(dataItem)}>Add to Cart</button>
        </div>
      ))}
      <h2>Your Cart</h2>
      <ul>
        {cart.map((item) => (
          <li key={item.id}>
            <p>{item.name} - ${item.price} </p>
            <p>Liter: {item.quantity}</p>
            <p>Unit price: {item.quantity * item.price}</p>
            <button onClick={() => addQuantity(item.id)}>+ Add more Liter(s)</button>
            <button onClick={() => reduceQuantity(item.id)}>- Reduce Liter(s)</button>
            <br />
            <button onClick={() => removeFromCart(item.id)}>Remove</button>
          </li>
        ))}
      </ul>
    </div>
  );
};

export default CartComponent;
Also in: https://stackoverflow.com/questions/77701758/nextjs-onclick-event-is-not-triggered-when-it-is-in-a-child-component
Answered by Jenn
Thanks, it was an overlapping styling. It was because of this:
 <span className="absolute inset-0" />
View full answer

11 Replies

Avatar
You added a use client at the top right
Avatar
I tried adding it on the cart component and I also tried not adding it, it still does not work
Avatar
Asian black bear
First step to make it work is to add the "use client" at the first line of the file
After it, do you see some error at console?
Avatar
no error on the console and I still can't click on it
Avatar
Replace click event handlers with console.log and check the logs in devtools
Avatar
Oh, I have noticed something, the button onClick does not work for anything that I have put inside this div:
 <div className="mx-auto grid max-w-2xl grid-cols-1 gap-x-8 gap-y-16 border-t border-gray-200 sm:mt-8 lg:mx-0 lg:max-w-none lg:grid-cols-2">
            <article className="flex max-w-xl flex-col items-start justify-between">
              //component here
            </article>
            <div className="flex max-w-xl flex-col items-start justify-between">
              <div className="group relative">
               
                <div className="flex max-w-xl flex-col items-start justify-between">
                  
                      <CartComponent
                        cart={cart}
                        addQuantity={addQuantity}
                        reduceQuantity={reduceQuantity}
                        removeFromCart={removeFromCart}
                        waterTypes={waterTypes}
                        addToCart={addToCart}
                      />
                    ) : (
                      <p>No data</p>
                    )}
                </div>              
              </div>
            </div>
Avatar
Probably some styling issues i guess
Avatar
I tried inserting this code and it won't let me click on the button to see the console output
 <div className="flex max-w-xl flex-col items-start justify-between">
                <button onClick={(e) => console.log(e)}>Click me</button>
                  {waterTypes ? (
                  //cart component here

                </div> 
Avatar
Use your inspector and see if something overlaps with it
Avatar
Thanks, it was an overlapping styling. It was because of this:
 <span className="absolute inset-0" />
Answer