0

I would like to integrate ReactJS as a component in Magento 2 for a custom module. In this module it should be possible to add items to the shopping cart.

How can I trigger Magento in ReactJS to execute Magento_Customer/js/customer-data => customer-data .reload(["cart"], true);

executes?

2 Answers 2

0

One possible solution would be to include an event trigger outside your React App in Magento which would trigger the behaviour.

define([
'jquery', 
'Magento_Customer/js/customer-data'
], function ($, customerData) {
  'use strict';

  return function () {
      customerData.initStorage();
      customerData.init();
      document.addEventListener('triggerAddToCart', function () {
          const sections = ['cart'];
          customerData.invalidate(sections);
          customerData.reload(sections, false);
      });
  }
});

Inside your React app you would then be able to trigger like this:

  document.dispatchEvent(new Event('triggerAddToCart'));

I hope this helps!

0

You can also create a window function which you can trigger inside react. e.g.

window.customCloseModal = function(){modalJquerySelector.modal('closeModal')};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.