TypeError: Cannot read properties of null (reading ‘vanta’)

huangapple go评论66阅读模式
英文:

React - TypeError: Cannot read properties of null (reading 'vanta')

问题

I learning React and building a simple portfolio in the process. I have incorporated a vanta.js animated background for my home screen, but after implementing a Navigation Bar, I am getting the error: TypeError: Cannot read properties of null (reading 'vanta'). The error happens when switching to a page that is not my Home page, which is the only page where the animated background should be visible.

This is my App.js:

import React from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Home from "./components/Home";
import Navbar from "./components/Navbar";
import Projects from "./components/Projects";

export default function App() {
  return (
    <main className="flex flex-col m-0 w-screen h-screen bg-gray-50 body-font text-gray-600 font-display">
      <BrowserRouter>
        <Navbar />
        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/projects" element={<Projects />} />
        </Routes>
      </BrowserRouter>
    </main>
  );
}

This is my Home.js, where the vanta.js background is incorporated. I don't quite understand why the vanta.js implementation from this file is accessed whenever I switch to a new page:

import React, { useRef, useEffect } from "react";
import * as THREE from "three";
import TRUNK from "vanta/dist/vanta.trunk.min";

export default function Home() {
    const myRef = useRef(null);

    useEffect(() => {
      TRUNK({
        THREE: THREE,
        el: myRef.current,
        mouseControls: true,
        touchControls: true,
        gyroControls: false,
        minHeight: 200.00,
        minWidth: 200.00,
        scale: 1.00,
        scaleMobile: 1.00,
        color: 0x3B82F6,
        backgroundColor: 0xF9FAFB
      });
  
      return () => {
        if (myRef.current.vanta) { // <-- ERROR HAPPENS HERE
          myRef.current.vanta.destroy();
        }
      };
    }, []);

    return (
      <section id="about" ref={myRef} className="flex-1">
        <div className="absolute container min-h-full min-w-full mx-auto my-auto bg-gray-50 z-10 lg:opacity-10 opacity-70"></div>
        <div className="container h-full w-full mx-auto my-auto flex px-10 py-20 md:flex-row flex-col items-center justify-center z-20">
          <div className="lg:flex-grow lg:w-1/2 lg:pr-24 flex flex-col lg:items-start lg:text-left mb-16 lg:mb-0 items-center justify-center text-center">
            <h1 className="title-font lg:text-5xl text-4xl mb-2 font-extrabold text-blue-500">
              NAME HERE
            </h1>
            <p className="mb-4 leading-relaxed lg:text-base text-sm font-normal">
              Developer
            </p>
          </div>
        </div>
      </section>
    );
  }

The error must be in one of the above files, but if needed, here is my Navbar.js:

import React from "react";
import {  Link } from "react-router-dom";

export default function Navbar() {
  return (
    <header className="bg-gray-50 top-0 z-10">
      <div className="container mx-auto flex p-5 flex-col items-end">
        <nav className="md:mr-4 md:py-1 md:pl-4 flex flex-wrap items-center text-base justify-center">
          <Link to="/" className="mr-5 hover:border-blue-500 border-2 px-4 py-1 rounded-full  hover:bg-blue-500 hover:text-white ease-in-out hover:ease-in-out duration-150">
            <p>Home</p>
          </Link>
          <Link to="/projects" className="mr-5 hover:border-blue-500 border-2 px-4 py-1 rounded-full hover:bg-blue-500 hover:text-white ease-in-out hover:ease-in-out duration-150">
            <p>Projects</p>
          </Link>
        </nav>
      </div>
    </header>
  );
}

Any help is appreciated, thanks!

英文:

I learning React and building a simple portfolio in the process. I have incorporated a vanta.js animated background for my home screen, but after implementing a Navigation Bar, I am getting the error: TypeError: Cannot read properties of null (reading &#39;vanta&#39;). The error happens when switching to a page that is not my Home page, which is the only page where the animated background should be visible.

This is my App.js:

import React from &quot;react&quot;;
import { BrowserRouter, Routes, Route } from &quot;react-router-dom&quot;;
import Home from &quot;./components/Home&quot;;
import Navbar from &quot;./components/Navbar&quot;;
import Projects from &quot;./components/Projects&quot;;
export default function App() {
return (
&lt;main className=&quot;flex flex-col m-0 w-screen h-screen bg-gray-50 body-font text-gray-600 font-display&quot;&gt;
&lt;BrowserRouter&gt;
&lt;Navbar /&gt;
&lt;Routes&gt;
&lt;Route path=&quot;/&quot; element={&lt;Home /&gt;} /&gt;
&lt;Route path=&quot;/projects&quot; element={&lt;Projects /&gt;} /&gt;
&lt;/Routes&gt;
&lt;/BrowserRouter&gt;
&lt;/main&gt;
);
}

This is my Home.js, where the vanta.js background is incorporated. I don't quite understand why the vanta.js implementation from this file is accessed whenever I switch to a new page:

import React, { useRef, useEffect } from &quot;react&quot;;
import * as THREE from &quot;three&quot;; // Importing THREE.js
import TRUNK from &quot;vanta/dist/vanta.trunk.min&quot;; // Importing the vanta effect
export default function Home() {
const myRef = useRef(null);
useEffect(() =&gt; {
TRUNK({
THREE: THREE,
el: myRef.current,
mouseControls: true,
touchControls: true,
gyroControls: false,
minHeight: 200.00,
minWidth: 200.00,
scale: 1.00,
scaleMobile: 1.00,
color: 0x3B82F6,
backgroundColor: 0xF9FAFB
});
return () =&gt; {
if (myRef.current.vanta) { // &lt;-- ERROR HAPPENS HERE
myRef.current.vanta.destroy();
}
};
}, []);
return (
&lt;section id=&quot;about&quot; ref={myRef} className=&quot;flex-1&quot;&gt;
&lt;div className=&quot;absolute container min-h-full min-w-full mx-auto my-auto bg-gray-50 z-10 lg:opacity-10 opacity-70&quot;&gt;&lt;/div&gt;
&lt;div className=&quot;container h-full w-full mx-auto my-auto flex px-10 py-20 md:flex-row flex-col items-center justify-center z-20&quot;&gt;
&lt;div className=&quot;lg:flex-grow lg:w-1/2 lg:pr-24 flex flex-col lg:items-start lg:text-left mb-16 lg:mb-0 items-center justify-center text-center&quot;&gt;
&lt;h1 className=&quot;title-font lg:text-5xl text-4xl mb-2 font-extrabold text-blue-500&quot;&gt;
NAME HERE
&lt;/h1&gt;
&lt;p className=&quot;mb-4 leading-relaxed lg:text-base text-sm font-normal&quot;&gt;
Developer
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/section&gt;
);
}

The error must be in one of the above files, but if needed, here is my Navbar.js:

import React from &quot;react&quot;;
import {  Link } from &quot;react-router-dom&quot;;
export default function Navbar() {
return (
&lt;header className=&quot;bg-gray-50 top-0 z-10&quot;&gt;
&lt;div className=&quot;container mx-auto flex p-5 flex-col items-end&quot;&gt;
&lt;nav className=&quot;md:mr-4 md:py-1 md:pl-4 flex flex-wrap items-center text-base justify-center&quot;&gt;
&lt;Link to=&quot;/&quot; className=&quot;mr-5 hover:border-blue-500 border-2 px-4 py-1 rounded-full  hover:bg-blue-500 hover:text-white ease-in-out hover:ease-in-out duration-150&quot;&gt;
&lt;p&gt;Home&lt;/p&gt;
&lt;/Link&gt;
&lt;Link to=&quot;/projects&quot; className=&quot;mr-5 hover:border-blue-500 border-2 px-4 py-1 rounded-full hover:bg-blue-500 hover:text-white ease-in-out hover:ease-in-out duration-150&quot;&gt;
&lt;p&gt;Projects&lt;/p&gt;
&lt;/Link&gt;
&lt;/nav&gt;
&lt;/div&gt;
&lt;/header&gt;
);
}

Any help is appreciated, thanks!

答案1

得分: 0

I managed to find a fix. There was an error in my implementation of vanta.js. 这个 用React实现的文档帮助了我。以下是更新后的代码,使其在我的 Home.js 中工作:

export default function Home() {
    const [vantaEffect, setVantaEffect] = useState(0); // 添加了这一行 + 导入 useState
    const myRef = useRef(null);

    useEffect(() => {
      if(!vantaEffect) { // &lt;-- 添加了这个条件
        TRUNK({
          THREE: THREE,
          el: myRef.current,
          mouseControls: true,
          touchControls: true,
          gyroControls: false,
          minHeight: 200.00,
          minWidth: 200.00,
          scale: 1.00,
          scaleMobile: 1.00,
          color: 0x3B82F6,
          backgroundColor: 0xF9FAFB
        });
      }

  
      return () => {
        if (vantaEffect) { // &lt;-- 在这里使用了 vantaEffect
          vantaEffect.destroy();
        }
      };
    }, []);

    return ( ... );
}
英文:

I managed to find a fix. There was an error in my implementation of vanta.js. This documentation of implementation with React helped me. Here is the updated code to make it work in my Home.js:

export default function Home() {
const [vantaEffect, setVantaEffect] = useState(0); // Added this + import useState
const myRef = useRef(null);
useEffect(() =&gt; {
if(!vantaEffect) { // &lt;-- Added this conditional
TRUNK({
THREE: THREE,
el: myRef.current,
mouseControls: true,
touchControls: true,
gyroControls: false,
minHeight: 200.00,
minWidth: 200.00,
scale: 1.00,
scaleMobile: 1.00,
color: 0x3B82F6,
backgroundColor: 0xF9FAFB
});
}
return () =&gt; {
if (vantaEffect) { // &lt;-- Used vantaEffect here
vantaEffect.destroy();
}
};
}, []);
return ( ... );
}

huangapple
  • 本文由 发表于 2023年4月14日 00:03:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定