Error: Attempting to access the text content of a null object

I recently followed a tutorial on text reveal animation and attempted to implement it in Next.Js. Unfortunately, I encountered an error message stating "TypeError: Cannot read properties of null (reading 'textContent')".

Here is the video tutorial link for reference: https://www.youtube.com/watch?v=_0yjWtaajgw

Additionally, when trying to use useRef, I still couldn't resolve the issue.

'use client';

import { useState, useRef, useEffect } from "react";

import styles from './title.module.css';

import anime from "animejs";

import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/dist/ScrollTrigger";


export default function Title() {

useEffect(() => {

    textWrapper = document.querySelector(`.${styles.title}`);
    textWrapper.innerHTML = textWrapper.textContent.replace(
        /\S/g,
        "<span className={styles.letter}>$&</span>"
    );

    anime.timeline().add({
        targets: ".${styles.title} .${styles.letter}",
        translateY: [-200, 0],
        easing: "easeOutExpo",
        duration: 1500,
    });
  

}, [])


    return (
        <section>
            <h1 className={styles.title}>Web Designer</h1>
        </section>
    )
}

Answer №1

Make sure to update the class name in the following code snippet:

let titleElement = document.querySelector("title");
titleElement.innerHTML = titleElement.textContent.replace(
    /\S/g,
    "<span className={styles.letter}>$&</span>"
);

<h1 className="title">Web Designer</h1>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enable the ability for anchor links to be clickable when the CSS media print format is used to generate

To illustrate: <a href="https://www.google.com">Google anchor link</a> on the website, it displays as: Google anchor link When printed, it shows as: Google anchor link(https://www.google.com) To address this issue, I included in the CSS: ...

Discover the row and column of a selected table cell using vanilla JavaScript, no need for jQuery

In my JavaScript code, I am currently working on creating an onclick function that will display the row and column of a specifically clicked cell in a table. I have successfully implemented functionality to return the column number when the cell is click ...

Ensure that the assistant stays beneath the cursor while moving it

I am currently working on creating a functionality similar to the 'Sortable Widget', but due to some constraints, I cannot use the premade widget. Instead, I am trying to replicate its features using draggable and droppable elements: $(".Element ...

Failure encountered when attempting to load JSON data into datalist

I've been trying to incorporate inputs into a datalist in two different ways. However, I've encountered an issue with the first method not working properly. --> Check it out on bootlply var dataList = document.getElementById('json-datal ...

Generate a list item that is contenteditable and includes a button for deletion placed beside it

I am attempting to create a ul, where each li is set as contenteditable and has a delete button positioned to the left of that li. My initial attempt looked like this: <ul id='list'> <li type='disc' id='li1' cl ...

Is there a way to automatically incorporate a component into every view within a Next.js application?

Is there a more efficient and less cumbersome way to import components for every view in a Next.js app? I am interested in incorporating the "arwes" framework into my project and utilizing components from . One of the examples of a component I will be usin ...

What is the best way to display asynchronous or observable data in Ionic framework?

I'm currently following a tutorial on LinkedIn Learning that covers implementing the design of a detail page with Ionic 4.0. The tutorial link can be found here. However, I've encountered an issue when trying to display the fetched data from the ...

Particle JS - Taking over the entire screen

I have incorporated Particle JS into the banner using the link provided. It should be confined within the banner, with a white background underneath displaying the header text "hello there." However, the Particle.JS effect is currently taking over the enti ...

Getting a variable from outside of the observable in Angular - a step-by-step guide

I have an Observable containing an array that I need to extract so I can combine it with another array. this.builderService.getCommercialData() .subscribe( data=>{ this.commercialDetails = data; this.commercialDetailsArr ...

Attempting to invoke a static function from a imported ES6 module

Currently, I am utilizing Firefox 56 with the setting dom.moduleScripts.enabled enabled. This configuration allows me to engage with native ES6 modules seamlessly. In my Vue2 component, there is a method that I have defined: import StorageZonesAjaxMethod ...

Discovering the object and its parent within a series of nested arrays

Is there a way to locate an object and its parent object within a nested array of unknown size using either lodash or native JavaScript? The structure of the array could resemble something like this: name: 'Submodule122'</p> I have been ...

Determining Adjacency of <li> Elements Using jQuery Sortable() and Class Comparison

I have created a custom display builder specifically for outdoor power equipment dealers. This tool allows them to easily add, remove, and rearrange different product display sections according to their preferences. To achieve this functionality, I utilize ...

ES6 Angular-Meteor ng-table's getData function is not being invoked

I've been working on updating my code to ES6. Specifically, I'm using angular-meteor and ng-table. Everything was displaying correctly in the table before the refactor, but now after converting to ES6 syntax, the data no longer appears. Here is a ...

Is there a way to create space at the bottom after setting margin to auto?

I'm currently mastering Bootstrap v5.0 and experimenting with creating a webpage using Bootstrap. I've encountered an issue that requires assistance. Specifically, I'm trying to add extra space between two columns on smaller screens while ma ...

Tips for handling an empty jQuery selection

Below is the code snippet: PHP CODE: if($data2_array[7]['status'] == "OK"){ $degtorad = 0.01745329; $radtodeg = 57.29577951; $dlong = ($lng - $supermercado_lng); $dvalue = (sin($lat * $degtorad) * sin($superm ...

Error encountered in sanity due to unidentified code type causing schema error

After using yarn to install the sanity code-input plug-in, I encountered an error. Error In my code, the type being used is "code". { name: 'exampleUsage', title: 'Example usage', type: 'code', }, ...

generate dynamic custom headers in an express application for accessibility by an Angular application

https://i.stack.imgur.com/6jyNE.pngRecently, I have started using Express and despite my extensive research, I haven't been able to find a solution to my issue. The problem is that I am receiving headers in my Express app, but when I attempt to make t ...

No results appearing in the output section

While developing a website using React, I encountered an error that said useState is defined but never used in the navbar component. To address this, I made changes to my ESLint configuration in the package.json file: "rules": { "eqeqe ...

Unable to show information in Next.js Strapi integration

I have set out to create a basic task management website as a way to delve into the world of full stack development. My tech stack includes Next.js and Strapi, but I've hit a roadblock. Despite my best efforts, I can't seem to get the server data ...

Extracting a substring using JavaScript within a PHP environment

Can anyone help me with this issue? I need to extract the ID from a link: www.imdb.com/title/tt5807628/ - > tt5807628 This is my javascript code: var str = "www.imdb.com/title/tt5807628/"; var n = str.search("e/tt"); var res = str.substring ...