ReactJS Script Function Not Exporting in Shopify Theme Extension

We are building an app in Shopify that has a button on product page that when pressed it opens a modal window. The button displays, the scritpt is loaded (console Network) but the handler for the button cannot find the function. It’s exported and the function is called in the app block for the theme extension.

iwtmodalbutton.liquid

{{ 'iwtmodalbutton.js' | asset_url | script_tag: 'module' }}

  {% render 'offer_button' %}

{% schema %}
{
  "name": "Offer Button",
  "target": "section",
  "settings": []
}
{% endschema %}

this is the function…iwtmodalbutton.js

'use client';

import React, { useState, useEffect } from 'react';
import { Checkbox } from '@shopify/polaris';
import {Modal} from 'react-modal';

console.log('iwtmodalbutton.js loaded');


export default function openOfferModal({ isOpen, onClose, onSubmit, isProductPage }) {
  const [formData, setFormData] = useState({
    firstName: '',
    lastName: '',
    email: '',
    phone: '',
    postalCode: '',
    offerAmount: '',
    agreedToTerms: false,
  });
  const [cartData, setCartData] = useState(null);

  useEffect(() => {
    // Retrieve cart contents from shopify
    fetch('/cart.js', {
        method: 'GET',
        headers: { 'X-Shopify-Access-Token': process.env.SHOPIFY_API_KEY}
        })
      .then((response) => response.json())
      .then((cart) => setCartData(cart))
      .catch((error) => console.error('Error fetching cart:', error));
  }, []);

  const handleChange = (field, value) => {
    setFormData({ ...formData, [field]: value });
  };

  const handleSubmit = () => {
    // Validate form fields
    if (!formData.agreedToTerms) {
      alert('Please agree to the terms of service.');
      return;
    }

    // Call the onSubmit callback with the form data
    onSubmit(formData);
  };

  const handleAddToCart = () => {
    if (isProductPage) {
      // Implement the logic to add the product to the cart here
      alert('Adding product to cart...');
    }
  };

  return (
    <Modal
      open={isOpen}
      onClose={onClose}
      title="Make an Offer"
      primaryAction={{
        content: 'Submit Offer',
        onAction: handleSubmit,
        disabled: !formData.agreedToTerms, // Disable button if terms not agreed
      }}
      secondaryActions={[{ content: 'Cancel', onAction: onClose }]}
    >
      <Modal.Section>
        <div className='modalWelcome'>
          <h1 className="header">Wanna Make a Deal?</h1>
          <p className="desc">Make an offer to buy your cart - give us our best shot!</p>
        </div>
        {cartData && (
          <table>
            <thead>
              <tr>
                <th>Product ID</th>
                <th>SKU</th>
                <th>Product Name</th>
                <th>Option</th>
                <th>Variant</th>
                <th>Quantity</th>
                <th>Price</th>
              </tr>
            </thead>
            <tbody>
              {cartData.items.map((item) => (
                <tr key={item.id}>
                  <td>{item.product_id}</td>
                  <td>{item.sku}</td>
                  <td>{item.product_title}</td>
                  <td>{item.variant_options[0]}</td>
                  <td>{item.variant_title}</td>
                  <td>{item.quantity}</td>
                  <td>{Shopify.formatMoney(item.price)}</td>
                </tr>
              ))}
            </tbody>
          </table>
        )}
        <div>
          {isProductPage && (
            <button onClick={handleAddToCart} className="addToCartButton">Add to Cart</button>
          )}
          <form className="formContainer">
            <input type="text" id="fname" placeholder="Your First Name" className="inputTXT" />
            <input type="text" id="lname" placeholder="Your Last Name" className="inputTXT" />
            <input type="email" id="email" placeholder="Your eMail" className="inputTXT" />
            <input type="tel" id="mobile" placeholder="Your Mobile Number" className="inputTXT" />
            <input type="number" id="offerAmount" placeholder="Make your offer" className="inputTXT" />
            <Checkbox checked={formData.agreedToTerms} onChange={(value) => handleChange('agreedToTerms', value)} la
            <input type="submit" id="submit" className="submitButton" value="Make Your Offer!"/>
          </form>
        </div>
      </Modal.Section>
    </Modal>
  );
};

Error code in console…

Uncaught ReferenceError: openOfferModal is not defined
at HTMLButtonElement.onclick (cart:1228:79)

I tried compiling the code using Babel. CommonJS use require and Shopify threw an error because the form should display to the client.

The above is the non-compiled function. Still poking around and opened support in Shopify forum but those answers are hit and miss.

New contributor

tbgmarketing is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật