如何从React元素获取ID

时间:2020-10-16 21:33:58

标签: reactjs

我不明白为什么,我无法从反应对象获取ID ...

public class TimerManager
    {
        private Timer _timer;
        private AutoResetEvent _autoResetEvent;
        private Action _action;

        public DateTime TimerStarted { get; }

        public TimerManager(Action action)
        {
            _action = action;
            _autoResetEvent = new AutoResetEvent(false);
            _timer = new Timer(Execute, _autoResetEvent, 1000, 2000);
            TimerStarted = DateTime.Now;
        }

        public void Execute(object stateInfo)
        {
            _action();

            if ((DateTime.Now - TimerStarted).Seconds > 60)
            {
                _timer.Dispose();
            }
        }
    }

我知道我做错了什么,但我不知道如何获取 id 来更改阶梯! 我已经尝试过在html页面中使用script标记,但是它没有用。

1 个答案:

答案 0 :(得分:0)

首先您的onClick必须是这样的:onClick={OpenNav}

但是我为您的问题提供了更好的解决方案,它正在使用Refs in Reat

例如,您可以像下面这样编写组件:

import React from "react";
import { useRef } from "react";

export const MayNav = () => {
  const MyRef = useRef();

  function OpenNav() {
    MyRef.style.animation = "MenuEntrar 0.5s";
  }

  return <div onClick={OpenNav} ref={MyRef}></div>;
};
相关问题