Customizing the Footer Copyright

Learn to override the copyright text at the bottom of the page

In this tutorial, we will override the component responsible for rendering the copyright text, and change that text to a different value. This will give you an insight into how the override mechanism works with React components. After completing this tutorial, you will be able to override other components as well.

Inspecting the Code

Before we can modify its code, we need to know which component is responsible for rendering the Footer. We can use the React developer tools to find out. Open the Components tab, and identify the element you want to override. In this case, it appears to be a child of the Footer component.

Now that we have found out the name of the component, there is only one place we need to look – the component directory in node_modules/@scandipwa/scandipwa/src/. Indeed, we can find a component named Footer there:

Original File in scandipwa/src/component/Footer/Footer.component.js
// [...]
export class Footer extends PureComponent {
    // [...]
    
    renderCopyrightContent() {
        const { copyright } = this.props;

        return (
            <ContentWrapper
              mix={ { block: 'Footer', elem: 'CopyrightContentWrapper' } }
              wrapperMix={ { block: 'Footer', elem: 'CopyrightContent' } }
              label=""
            >
                <span block="Footer" elem="Copyright">
                    { copyright }
                    { ' Powered by ' }
                    <a href="https://scandipwa.com">
                        ScandiPWA
                    </a>
                </span>
            </ContentWrapper>
        );
    }

    render() {
        return (
            <footer block="Footer" aria-label="Footer">
                { this.renderContent() }
                { this.renderCopyrightContent() }
            </footer>
        );
    }
}

export default Footer;

That funny HTML-in-JavaScript syntax is called JSX. Scandi uses the React library to render its user interface, and JSX is the easiest way to use React.

Now that we have found the component, we can update its code. But don't edit the file we found in node_modules – modifying dependency code is almost always a bad idea. (It would be hard to get updates, and difficult to track which of the many files you have edited). Instead, let's override it.

Scandi offers a great way to customize any component, and its called the Override Mechanism. With the override mechanism, you can override any file you want, while keeping the default implementations for the other files. This gives you great flexibility without having to duplicate any code.

To override a file, you need to create a new file with the same path in your src directory. For example...

To override:
    component/Footer/Footer.component.js
    (in node_modules/@scandipwa/scandipwa/src/)

...you need to create a new file:
    component/Footer/Footer.component.js
    (in src/)

Instead of manually creating a new file, you can save yourself a lot of time by using the Scandi CLI. With a single command, you can override the Footer component:

scandipwa override component Footer

When you run that command, the Scandi tool will ask you which components you want to override. Select the Footer class in Footer.component.js; leave the other fields blank:

Now we have created the file, which overrides the original Footer component file. Now, we want to import the original class, so that we can keep most of the Footer functionality, and extend it to customize its behavior:

File override in src/component/Footer/Footer.component.js
// We will need the ContentWrapper component later - let's import it
import ContentWrapper from 'Component/ContentWrapper';


// Import the original class (we want to keep most of the functionality)
// Note that we are using the "SourceComponent" alias in the import path –
// This tells Scandi that we want to get the original Footer component
import {
    Footer as SourceFooter
} from 'SourceComponent/Footer/Footer.component';


// Extend the original class (SourceFooter)
// By subclassing it, we can change some of its behavior
/** @namespace myFirstApp/Component/Footer/Component/FooterComponent */
export class FooterComponent extends SourceFooter {

    // This is the function responsible for rendering copyright
    // We want to change it, so we re-define in this subclass
    renderCopyrightContent() {
    
        // Changed:
        // Instead of the copyright text, let's write a friendly message
        return (
            <ContentWrapper
              mix={ { block: 'Footer', elem: 'CopyrightContentWrapper' } }
              wrapperMix={ { block: 'Footer', elem: 'CopyrightContent' } }
              label=""
            >
                <span block="Footer" elem="Copyright">
                    Thank you for visiting my website. You are amazing!
                </span>
            </ContentWrapper>
        );
    }
    
    // All the other functions will stay the same...
    // Because we didn't override any other default functionality
}

// All components, including the original Footer component, have a
// default export. Other files use this export when they want to use
// this component.
// Now, instead of providing the original component, we export our
// overridden component. Any file importing this will get the new behavior!
export default FooterComponent;

Results

Scandi implements hot reload – which means that you don't need to compile the app again. Just check your browser and the changes should have appeared.

Congratulations! Now you understand the basics of file overriding – and you can override any file in the app to change its behavior.

Last updated