如何在 React 中使用 Typed.js

如何在 React 中使用 Typed.js

React 现在是一个流行的、强大的、非常受欢迎的 Web 开发库。

今天,给大家分享以下 Typed.js在React中的使用

安装

npm install typed.js #npm
yarn add typed.js #yarn
pnpm add typed.js #pnpm

使用

首先引入Typed.js

import Typed from "typed.js"

为了使用Typed.js,并且让他在React中工作,我们还需要两个钩子,一个用于引入DOM元素,一个用来执行副作用。

示例

import {useRef,useEffect} from "rect"
export default function Home(){
    const el=useRef(null);
    useEffect(()=>{
        const typed = new Typed(el.current,{
            strings: [
                "字符串1",
                "字符串2",
              ],
              startDelay: 300,
              typeSpeed: 100,
              backSpeed: 100,
              backDelay: 100,
              loop: true,
        });
        return ()=>{
            typed.destroy();
        }
    })
    return (
    <div>
            <h1 ref={el}></h1>
    </div>
    )
}
如何在 React 中使用 Typed.js
https://pic.imgdb.cn/item/648928941ddac507cc4e2a0a.webp
作者
Firefly
发布于
2026-04-03
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
Firefly
Hello, I'm Firefly.
公告
欢迎来到我的博客!这是一则示例公告。
音乐
暂无封面

音乐

暂未播放

0:000:00
暂无歌词
暂无歌曲
分类
标签
站点统计
文章
21
分类
3
标签
13
总字数
57,102
运行时长
601
最后活动
143 天前

目录