ScandiPWA
Create Magento AppCreate ScandiPWA AppUser ManualGitHub
  • Why Scandi
  • πŸš€Quick-start Guide
  • πŸ—ΊοΈRoadmap
  • Introduction to the Stack
    • CMA, CSA, and ScandiPWA
    • Challenges
  • Setting up Scandi
    • Storefront Mode Setup
      • Proxying requests to server
    • Magento Mode Setup
    • Existing Magento 2 setup
    • Magento Commerce Cloud setup
    • Updating to new releases
      • Storefront mode upgrade
      • Magento mode upgrade
      • CMA upgrade
      • CSA upgrade
      • Custom ScandiPWA composer dependency update
      • Local ScandiPWA Composer Package Setup
    • Docker Setup [deprecated]
      • Legacy Docker setup
      • Migrating to CMA & CSA
  • Developing with Scandi
    • Override Mechanism
      • Overriding JavaScript
        • Overriding classes
        • Overriding non-classes
      • Overriding Styles
      • Overriding the HTML / PHP
      • Parent Themes
    • Extensions
      • Creating an extension
      • Installing an extension
      • Migrating from 3.x to 4.x
      • Publishing an extension
      • Extension Terminology
    • Working With Magento
      • Magento troubleshooting
      • Working with Magento modules
      • Working with GraphQL
      • GraphQL Security
      • Working with "granular cache"
    • Developer Tools
      • Debugging in VSCode
      • ScandiPWA CLI
      • Configuring ESLint
      • CSA Commands
    • Deploying Your App
      • Build & Deploy Android app
      • Build & Deploy iOS app
  • Structure
    • Directory Structure
    • Building Blocks
      • Components
        • Styling Components
      • Routes
      • Redux Stores
      • GraphQL Queries
      • Global Styles
      • The Util Directory
      • Type Checking
    • Application assets
    • Code Style
      • JavaScript Code Style
      • SCSS Code Style
  • Tutorials
    • Customizing Your Theme
      • Styling
        • Customizing the Global Styles
        • Adding a New Font
        • Overriding a Components Styles
        • Extending a Component's Styles
      • Customizing JavaScript
        • Customizing the Footer Copyright
        • Adding a New Page
        • Adding a Section in My Account
        • Adding a Tab on the Product Page
        • Creating a New Redux Store
    • Payment Method Integration
      • Setting Up for Development
      • Redirecting to the Payment Provider
      • Handling the Customer's Return
    • Creating a Custom Widget
      • Scandi CMS System Overview
      • Creating a Magento Widget
      • Implementing the Rendering
    • Video Tutorials
      • #1 Setting up and talking theory
      • #2 Templating in React
      • #3 Overriding a file
      • #4 Styling the application
      • #5 Patterns of ScandiPWA
    • Dark Mode Extension
    • Deploying Native Apps
    • Product 3D Model Extension
      • Part 1: Magento 3D Model Uploads
      • Part 2: GraphQL API
      • Part 3: Scandi Frontend
    • Social Share, Full Extension Development
      • STEP-1 and 2 Creating Magento 2 Module
      • STEP-3 Backend Configurations Settings
      • STEP-4 Simple GraphQl and Resolver
      • STEP-5 Creating Extension, Base Redux Store
      • STEP-6 Extension plugins
      • STEP-7 GraphQL types, Helpers
      • STEP-8 Query Field and FieldList
      • STEP-9 render Plugins and MSTP Plugin, Component creation
      • STEP-10 SocialShare Component Development
      • STEP-11 SocialShare for CategoryPage
      • TASK-1 Changing LinkedIn to Twitter
      • STEP-12 Comments for Admin Users
      • STEP-13 Final, bugfixes
    • Accessing Magento 2 Controllers
      • STEP-1 Creating Magento 2 Module
      • STEP-2 - Create Magento 2 Frontend Route and Basic Controller
      • STEP-3 Accessing Magento 2 Controller, Bypassing ScandiPWA frontend
      • STEP-4 Creating ScandiPWA Extension with additional dependencies
      • STEP-5 Creating Plugin and Axios request
  • About
    • Support
    • Release notes
    • Technical Information
    • Data Analytics
    • Contributing
      • Installation from Fork
      • Repository structure
      • Code contribution process
      • Submitting an Issue
      • Publishing ScandiPWA
Powered by GitBook
On this page
  • 1. Do not copy files
  • 2. Call original functions if possible

Was this helpful?

  1. Developing with Scandi
  2. Override Mechanism
  3. Overriding JavaScript

Overriding non-classes

Virtually always, you will want to keep part of the default functionality and only modify some aspect of the existing file. This is how the theme is usually extended.

This is easy-to-achieve with class, where you can extend the original file providing some custom functionality. But what to do if an override of the constant or function is intended?

Following are the rules, which will help you to stay "compatible" (thus ensuring easier upgrades):

1. Do not copy files

Instead of copy-pasting a file, prefer manually exporting original functions and constants, and then re-exporting ones you changed!

The ScandiPWA import mechanism provides the Source aliases to enable you to directly import from the parent theme, regardless of whether there is an overridden version available. Similarly, you can use the SourceRoute alias to import directly from a route defined in the parent theme, or the SourceUtil alias to import a utility file from the parent theme.

Let's now consider an example. Here, we would like to override a MIN_PASSWORD_LENGTH defined in the component/Form/Form.config.js file. Here is how we can do it (without copying the file):

override: component/Form/Form.config.js
// exporting all functions and constants from original file
export * from 'SourceComponent/Form/Form.config.js';
// specifically exporting default (as it is not included in "*")
export { default } from 'SourceComponent/Form/Form.config.js';
// re-exporting the overriden variable
export const MIN_PASSWORD_LENGTH = 6;

2. Call original functions if possible

Instead of copying the original function from the source file, consider importing and calling it, then processing the original output value.

Let's now consider an example. Here, we would like to override a mapDispatchToProps defined in the component/AddToCart/AddToCart.container.js file. Here is how we can do it (without copying the original function):

Heads up!

When overriding a function that is used later in the component (for example in default export), you must also override the place the function is being used (therefore, as per example, default export).

overriding: component/AddToCart/AddToCart.container.js
// imporing original function to call
import {
    AddToCartContainer,
    mapStateToProps,
    mapDispatchToProps as sourceMapDispatchToProps
} from 'SourceComponent/AddToCart/AddToCart.container.js';

// importing to replicate original default export
import { connect } from 'react-redux';

// exporting all functions and constants from original file
export * from 'SourceComponent/AddToCart/AddToCart.container.js';

// re-exporting the overriden variable
export const mapDispatchToProps = (dispatch) => {
    // calling the original function to modify the result
    const handler = sourceMapDispatchToProps(dispatch);

    // add custom functionality    
    handler.logProduct = () => {
        console.log('product added to cart');
    };
    
    // returning modified result
    return handler;
}

// exporting default
// (becuase function mapDispatchToProps was used in it)
export default connect(mapStateToProps, mapDispatchToProps)(AddToCartContainer);
PreviousOverriding classesNextOverriding Styles

Last updated 3 years ago

Was this helpful?