Integrating Components
To integrate Components to your frontend, you will leverage two tools:
Wallet Selector: Enables the user to select their preferred NEAR wallet in your dApp.NEAR VM: A package that can retrieve the component's code from the blockchain and execute it in the browser.
Using those tools you will implement the following flow:
- Setup the VM.
- Render components using the
Widgetcomponent in the VM. - Setup a wallet selector so users can interact with the Menu.
Adding the VM & Wallet Selector
To use the VM and the wallet-selector, you must add them to your project first.
The wallet selector has multiple wallet packages to select from. Check their website for more information.
npm install \
@near-wallet-selector/core \
@near-wallet-selector/my-near-wallet \
@near-wallet-selector/modal-ui
Then, manually add the VM to your package.json:
"dependencies": {
...
"near-social-vm": "github:NearSocial/VM#2.5.5"
...
}
팁
Check the latest released version for the VM here
Setup the VM
To render components, you need to import the useInitNear hook from the near-social-vm package, as well as the Widget component.
import { useInitNear, Widget } from 'near-social-vm';
import { useEffect } from 'react';
export default function Component({ src }) {
const { initNear } = useInitNear();
useEffect(() => {
initNear && initNear({ networkId: 'testnet', selector: null });
}, [initNear]);
return <Widget src={src} />;
}
return <Component src="influencer.testnet/widget/Greeter" props={{name: "Anna", amount: 2}} />
팁
Notice that the VM is inherently linked to React, so you will need to use a reactive framework to take full advantage of the VM.