intro.js 的 React 包装器无法识别元素

问题描述 投票:0回答:2

我正在为 intro.js 使用 React 包装器。我正在尝试将其集成到功能组件中。这些步骤工作正常,但 intro.js 无法正确识别步骤中提供的元素。我尝试使用 document.getElementById() 和 document.querySelector() 定义元素。似乎没有任何效果。还有其他办法吗?

import React,{useState} from 'react'

import { Steps, Hints } from "intro.js-react";

import "intro.js/introjs.css"

import "intro.js/themes/introjs-nassim.css";

const AppIntro = () =>{

const [stepsEnabled, setStepsEnabled] = useState(false)
const [initialStep, setInitialStep] = useState(0)
const [steps, setSteps] = useState([
    {
        intro:"Welcome to our application",
        position: 'top'
    },
    {
        element:document.getElementById('.card'),
        intro: 'This is the second step',
        position: 'left'
    },
    {
        element: document.querySelector('.tableRow'),
        intro:'This is the third step',
        position: 'top'
    }
])
 const onExit = () =>{
    setStepsEnabled(false);
  }
const startIntro=()=>{
    setStepsEnabled(true)
}
return(
    <div>
    <button onClick={()=>startIntro()}>Click me</button>
    <Steps
         enabled={stepsEnabled}
         steps={steps}
         initialStep={initialStep}
         onExit={onExit}
         options=
         {{
             tooltipClass: 'customTooltip',
             showProgress: true,
             showStepNumbers: true,
             showBullets: false,
             exitOnOverlayClick: false,
             doneLabel: "Done",
             nextLabel: "Next",
             hideNext: false,
         }}
     />
     </div>
)

} 导出默认AppIntro

这是我的组件

javascript reactjs intro.js
2个回答
0
投票

你需要更换

    {
        element:document.getElementById('.card'),
        intro: 'This is the second step',
        position: 'left'
    },
    {
        element: document.querySelector('.tableRow'),
        intro:'This is the third step',
        position: 'top'
    }

    {
        element: '.card',
        intro: 'This is the second step',
        position: 'left'
    },
    {
        element: '.tableRow',
        intro:'This is the third step',
        position: 'top'
    }

这会将步骤附加到具有 .card 和 .tableRow 类的元素。如果您想使用 ID 来标识要附加的元素,可以将 '.card' 替换为 '#card',以便附加到具有卡 id 的元素。


0
投票

问题可能是当您初始化 introJS 时,元素不存在于 DOM 中,因此不会附加到 introJS 实例。你必须把它拆掉并重建它。

  const handleTabChange = (startStepIndex: number): void => {
if (stepsRef.current !== null) {
  setTimeout(() => {
    stepsRef.current?.introJs.start();
    stepsRef.current?.introJs.goToStepNumber(startStepIndex);
  }, 1000);
}

};

这就是我采取的方法。每当我更改选项卡并加载新的 dom 元素时,我都会重新启动 introJS 实例并直接转到适当的步骤。

© www.soinside.com 2019 - 2024. All rights reserved.