The following software is required to run the sample:
- Git
- Node.js
- NPM
Clone the repository:
git clone https://github.com/ArtOfMicrofrontends/11-frontend-balance.git
Go to the repository's directory and run NPM install:
npm install
Now start the application:
npm start
Follow these steps to implement the same from scratch.
- Initialize a new project and add the dependencies
npm init -y
npm install react react-dom --save
npm install @babel/core @babel/preset-env @babel/preset-react babel-loader file-loader style-loader webpack webpack-cli --save-dev
-
Add a webpack.config.js and make sure to target
library
(with the name of the package) -
Add a .babelrc for Babel using the
preset-react
:
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
-
Copy over the balance sheet code from the previous sample
-
Change the
index.jsx
to export asetup
function
export function setup(api) {
let BalanceSheet = undefined;
api.registerPage("/", {
bootstrap: () =>
import("./BalanceSheet").then((content) => {
BalanceSheet = content.BalanceSheet;
}),
mount: (target) => render(<BalanceSheet onRender={api.renderExtension} />, target),
unmount: (target) => render(null, target),
});
}
-
Change the
BalanceSheet
to forward theonRender
prop, pointing to therenderExtension
API -
Use the
onRender
prop in theMoreBalanceInfo
component:
const MoreBalanceInfo = ({ onRender, ...props }) => {
const ref = React.useRef(null);
React.useEffect(() => {
return onRender(ref.current, "balance-info", props);
}, []);
return <slot ref={ref} />;
};
- Build the MF with Webpack (
npx webpack --mode production
) and then publish it using
npm pack
curl -F 'file=@./balance-1.0.0.tgz' http://localhost:9000/modules
rm *.tgz
where localhost:9000
is the address of the feed server