Iām working on a project and Iām getting an error NoMoralisContextProviderError: Make sure to only call useMoralis within a <MoralisProvider>
Iām working on FreeCodeCampās Solidity course and following the code of that course exactly, so I know the code should work, but it isnāt even with the same versions. Someone posted a similar issue here where they changed there react-moralis
verison from ā^1.3.5ā to ā1.3.5ā and back, but that didnāt work for me (AKA I tried everything in this thread). It has something to do with the MoralisProvider server connection because when I turn initializeOnMount={false}
in the MoralisProvider it removed the error. Here is my code:
_app.js
import { MoralisProvider } from "react-moralis"
import Header from "../components/Header"
import Head from "next/head"
const APP_ID = process.env.NEXT_PUBLIC_APP_ID
const SERVER_URL = process.env.NEXT_PUBLIC_SERVER_URL
function MyApp({ Component, pageProps }) {
return (
<div>
<Head>
<title>NFT Marketplace</title>
<meta name="description" content="NFT Marketplace" />
<link rel="icon" href="/favicon.ico" />
</Head>
<MoralisProvider initializeOnMount={false} serverUrl={SERVER_URL} appId={APP_ID}>
<Header />
<Component {...pageProps} />
</MoralisProvider>
</div>
)
}
export default MyApp
Header.js
(component)
import { ConnectButton } from "web3uikit"
import Link from "next/link"
export default function Header() {
return (
<nav className="p-5 border-b-2 flex flex-row justify-between items-center">
<h1 className="py-4 px-4 font-bold text-3xl">NFT Marketplace</h1>
<div className="flex flex-row items-center">
<Link href="/">
<a className="mr-4 p-6">Home</a>
</Link>
<Link href="/sell-nft">
<a className="mr-4 p-6">Sell NFT</a>
</Link>
<ConnectButton moralisAuth={true} />
</div>
</nav>
)
}
index.js
import Image from "next/image"
import styles from "../styles/Home.module.css"
import { useMoralisQuery } from "react-moralis"
export default function Home() {
const { data: listedNfts, isFetching: fetchingListedNfts } = useMoralisQuery(
"ActiveItem",
(query) => query.limit(10).descending("tokenId")
)
console.log(listedNfts)
return <div className={styles.container}>Hi!</div>
}
package.json
dependencies:
"dependencies": {
"moralis": "1.5.11",
"next": "12.1.5",
"react": "18.1.0",
"react-dom": "18.1.0",
"react-moralis": "1.3.5",
"web3uikit": "^0.0.133"
},
"devDependencies": {
"autoprefixer": "^10.4.5",
"dotenv": "^16.0.0",
"eslint": "8.14.0",
"eslint-config-next": "12.1.5",
"postcss": "^8.4.12",
"tailwindcss": "^3.0.24"
}