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!
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):
// 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):
Last updated
Was this helpful?