#Button onClick cannot be click when it is placed as a child component

1 messages · Page 1 of 1 (latest)

glass mist
#

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}>
          <strong>Name:</strong> {dataItem.name}, <strong>Price:</strong> {waterType.price}
          <button onClick={() => addToCart(dataItem)}>Add to Cart</button>
        </div>
      ))}
      <div>
        Your Cart
      </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;
cyan acornBOT
#

Hi @glass mist. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the extracts, there are a few potential issues that could be causing your onClick events not to trigger.

  1. Client-side JavaScript not loading: As mentioned in the Github issue 46123 (https://github.com/vercel/next.js/issues/46123), there could be an issue with client-side JavaScript not loading correctly. This could prevent your onClick handlers from being registered and therefore not triggering when clicked.

  2. Hydration issues: In Github issue 45047 (https://github.com/vercel/next.js/issues/45047), it's mentioned that interacting with a client component before the page fully loads can cause hydration errors. This could potentially be causing your onClick events not to trigger.

  3. Issues with functional components as children: According to the Next.js documentation (https://nextjs.org/docs/pages/api-reference/components/link), if the child of a Link is a functional component, you must wrap the component in React.forwardRef and pass onClick, href, and ref to the DOM element for proper handling.

Without more specific information about your setup, it's hard to pinpoint the exact issue. However, you could try the following:

  • Ensure that your client-side JavaScript is loading correctly.
  • Check for hydration errors and ensure that your page is fully loaded before interacting with client components.
  • If you're using functional components as children, make sure to wrap them in React.forwardRef and pass the necessary props to the DOM element.

If none of these solutions work, it might be helpful to provide more information about your setup or consider opening an issue on the Next.js GitHub page for more specific help.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: