Lab - Create a Mini App
Lab - Create a Mini App
Lab - Create a Mini App
This lab will walk you through setting up a local environment running a lean, single-page React app to integrate with a Stencil storefront and the Buyer Portal app. You’ll use this app to enhance your storefront using data provided by the Buyer Portal.
For following along in the code in future lessons, it’s also helpful to make sure you have a code editor, like VS Code, to open your project once installed.
Node Version Manager (nvm) is the recommended way to install Node.js and allows you to install and use different versions.
With nvm installed, you can install a specific version:
nvm install 20
… and switch to the Node.js runtime of any version you have installed:
nvm use 20
Whether you use nvm or install Node.js via another method, make sure you are running a compatible version:
node -v
The application you’ll be setting up is a small, single-page React app. There are multiple frameworks available for building single-page apps. For our use case, we only need to be able to inject React components into an existing page, so we’re using one of the simplest options. The app is bootstrapped using Vite, a popular build tool for modern web projects.
The setup for a similar app of your own is simple, as described in React documentation. Our mini-app was created with npm create vite@latest -- --template react-ts.
The app’s starting state has been stripped back further from the Vite starter template, including removing the default HTML page and App component, as well as tweaking Vite config to make main.tsx an explicit entry point and allow cross-origin requests to the dev server.
See the initial modifications here.
degit.degit is a tool for making copies of Git repositories.
From here on, we’ll leave managing your project’s source control to you.
Take note of the localhost port displayed in your terminal output. (e.g., http://localhost:5173) Port 5173 is typical, but your port might be different if this port is already taken.
In order to load and initialize the app in your storefront, we’ll take the same strategy as the Buyer Portal itself, using Script Manager.
![]()
Troubleshooting
If you don’t see the right message in the console:
src/main.tsx should contain console.log('B2B custom app initialized');http://localhost:5173/src/main.tsx) in your browser to verify it is served correctly.Our mini-app will need to access the window.b2b object but can only do so after the Buyer Portal has initialized. The cleanest way to handle this is to centralize the logic into a custom React hook.
src/hooks/useB2B.ts and implement the logic in the existing useB2B hook.useB2B.ts contains some simple type definitions to let TypeScript know what window.b2b looks like. The hook we’ve implemented simply uses a check on a short interval to set a piece of React state as soon as window.b2b.utils is available.
With this hook in place, any component in the app simply needs to invoke it to access b2b and all its methods. By virtue of standard React patterns, any such components will re-render as necessary as soon as the state value in the hook is updated. These componens won’t need to concern themselves with the details of waiting for the Buyer Portal to initialize.
src/main.tsx and remove the console.log statement.The above code creates a root element and appends it to the bottom of the page, then uses a standard pattern to render React into that element.
With this in place, the messsage logged to your browser console should now be “B2B utils are available”, indicating that the Buyer Portal is initialized and the app is ready to use.