Adding a Section in My Account
Learn to override with a common pattern in Scandi β render maps
Last updated
Learn to override with a common pattern in Scandi β render maps
Last updated
In Scandi, several UI elements, including the My Account page, are organized in tabs:
In this tutorial, we will learn to customize the theme by adding a new tab to the My Account page. If you follow along with this tutorial, you should be able to add any component to any tab in Scandi.
First, we need to create a component responsible for displaying the content of the tab. This means creating a new subdirectory in the component
directory with the required files. Using the scandipwa
CLI utility, this step can be automated with a single command.
We will name our example component PositiveAffirmations
:
This should create some new files and give the following output:
Now, let's edit the .component.js
file to render some example content. For simplicity, our example will contain positive self-affirmations copy-pasted from the internet:
This will help customers gain confidence and boost their mood while they adjusting the settings for their account.
Wondering what the { __("") }
magic is for? They are necessary to enable translating the text. You could remove them, and the app would still work, but only in English. Let's break down what that syntax does:
The curly braces {}
allow us to escape from JSX (that HTML-like syntax) and write normal JavaScript expressions.
__
is a special function-like directive in Scandi. It allows us to translate the content.
The text needs to be put in quotes ""
so that it can be processed like any other JavaScript string.
Great! Now we have created the component, but it's not yet visible on the page, because we haven't used it anywhere β that's the next step.
To add a new tab, we need to find out which component is responsible for rendering all of the My Account tabs. Using React Developer tools, we can identify the MyAccountTabList
component:
Inspecting the source code and searching for usages of <MyAccountTabList
, we find that the tabs are passed as props from MyAccount.component.js
The tab components themselves are specified in the renderMap
:
The metadata (such as the tab name), is passed to the component as a prop from MyAccount.container.js
, where the tabMap
is defined:
We need to override the .container
to add a new tab entry, but we also need to override the .component
to specify how to render it.
As we found above, we need to override the MyAccount
route to customize the tabs. Again, the scandipwa
CLI can do this automatically for us. First, enter this command:
Then, the scandipwa
CLI will interactively ask you which parts of the MyAccount
route you want to override. We only want to override the MyAccountContainer
class in the MyAccount.container.js
file, and the MyAccount
class in the .component
file; leave the other answers blank.
The command will automatically create the boilerplate necessary for overriding the file. Now, it's up to us to make the necessary changes in these files.
First, open the MyAccount.component
file and configure the renderMap
to include our custom component.
Now, MyAccountComponent
knows how to render our custom tab. We still need to configure the tab URL and title for it to appear in the list. For this, we can override the tabMap
field of the MyAccountContainer
.
Scandi is very strict about code style, and you might have to fix some issues for the ESLint check to pass. Most issues can be automatically fixed by running eslint --fix src
.
Let's check the brand-new My Account tab β as expected, it appears among the other tabs!
Congrats! You have now learned to add new tabs to Scandi components. And you will find that similar patterns with renderMap
can be overridden in the same way β you can add, remove, or modify which components are rendered by configuring an object or array.