21
loading...
This website collects cookies to deliver better user experience
create-react-app
. Open your terminal and run the following command:npx create-react-app nft-collectible-frontend
cd nft-collectible-frontend
npm start
public/index.html
and change the title and meta description of your website. This step is optional.App.test.js
, logo.svg
, and setupTests.js
files. We will not be needing these files for this tutorial.App.js
file and replace its contents with the following boilerplate.import './App.css';
function App() {
return (
<h1>Hello World</h1>
);
}
export default App;
App.css
as well. Do not, however, delete this file. In a later section, we will provide you with some basic styling that should be good enough for this demo project.artifacts/contracts/NFTCollectible.sol/NFTCollectible.json
.contracts
in the src
folder and paste the NFTCollectible.json
file.App.js
file.App.js
and all our CSS in App.css
App.js
file.import { useEffect } from 'react';
import './App.css';
import contract from './contracts/NFTCollectible.json';
const contractAddress = "0x355638a4eCcb777794257f22f50c289d4189F245";
const abi = contract.abi;
function App() {
const checkWalletIsConnected = () => { }
const connectWalletHandler = () => { }
const mintNftHandler = () => { }
const connectWalletButton = () => {
return (
<button onClick={connectWalletHandler} className='cta-button connect-wallet-button'>
Connect Wallet
</button>
)
}
const mintNftButton = () => {
return (
<button onClick={mintNftHandler} className='cta-button mint-nft-button'>
Mint NFT
</button>
)
}
useEffect(() => {
checkWalletIsConnected();
}, [])
return (
<div className='main-app'>
<h1>Scrappy Squirrels Tutorial</h1>
<div>
{connectWalletButton()}
</div>
</div>
)
}
export default App;
App.css
file..main-app {
text-align: center;
margin: 100px;
}
.cta-button {
padding: 15px;
border: none;
border-radius: 12px;
min-width: 250px;
color: white;
font-size: 18px;
cursor: pointer;
}
.connect-wallet-button {
background: rgb(32, 129, 226);
}
.mint-nft-button {
background: orange;
}
ethereum
object into your browser’s global window
object. We will be accessing window.ethereum
to perform the bulk of our functionality.checkWalletIsConnected
function within the App
component that checks if the Metamask wallet exists.useEffect
hook that checks Metamask’s existence when the App component loads.window.ethereum.request
method.App()
with the useState hook that will keep track of the user’s wallet address. (Don’t forget to import useState
from React!)const [currentAccount, setCurrentAccount] = useState(null);
connectWalletHandler
function.{currentAccount ? mintNftButton() : connectWalletButton()}
currentAccount
state only within the connectWallet
function.App
component is loaded (i.e every time we refresh).checkWalletIsConnected
function to check for accounts as soon as the website is loaded and set currentAccount if the wallet has already been connected.ethers
library from our smart contract project. In your terminal, run the following command:npm install ethers
App.js
.import { ethers } from 'ethers';
mintNftHandler
function.async
)