Add class dynamically for react object (when dragged) 在拖动时动态添加React对象的类

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

Add class dynamically for react object (when dragged)

问题

我想要将拖动的对象保持在其他对象的顶部。

所以,

我有一个名为 basic.module.css 的 CSS 文件

.dragging-shelf {
  z-index: 100;
}

然后在函数中

import styles from "../css/basic.module.css";

const [isDragging, setIsDragging] = useState(false);

const handleStart = (e, data) => {
    console.log("drag before:", data.node.classList);
    data.node.classList.add("dragging-shelf");
    console.log("drag after:", data.node.classList);
    setIsDragging(true);
}

const handleStop = (e, data) => {
    console.log("drag handle stop e:", e);
    console.log("drag handle stop data::", data);
    data.node.classList.remove("dragging-shelf");
    setIsDragging(false);
}

return (
    <Draggable onStart={handleStart} onDrag={handleDrag} onStop={handleStop}>
        <div className='handle'>
            <MyObj></MyObj>
        </div>
    </Draggable>
);

我可以确认 dragging-shelf 已经通过 console.log 添加到 data.node.classList 中。

然而,这在浏览器上没有反映出来。

我的拖动对象在其他对象的后面。

英文:

I want to keep the dragged object on the top of other objects.

So,

I have css basic.module.css

.dragging-shelf {
  z-index:100;
}

then in function

import styles from &quot;../css/basic.module.css&quot;;

const [isDragging, setIsDragging] = useState(false)
const handleStart = (e,data) =&gt;{
    console.log(&quot;drag before:&quot;,data.node.classList);
    data.node.classList.add(&quot;dragging-shelf&quot;);
    console.log(&quot;drag after:&quot;,data.node.classList); 
    setIsDragging(true);
}
const handleStop = (e,data) =&gt;{
    console.log(&quot;drag handle stop e:&quot;,e);
    console.log(&quot;drag handle stop data::&quot;,data);
    data.node.classList.remove(&quot;dragging-shelf&quot;);
    setIsDragging(false);
}

 return &lt;Draggable onStart={handleStart} onDrag={handleDrag} onStop={handleStop}&gt;   
         &lt;div className=&#39;handle&#39;&gt;
          &lt;MyObj&gt;&lt;/MyObj&gt;&lt;/div&gt;
         &lt;/Draggable&gt;

I can confirm dragging-shelf is added to data.node.classList with console.log.

However this is not reflected on browser.

My Dragged obj is behind another objs.

答案1

得分: 1

In ReactJS,每当你想要动态创建一组类时,你可以使用诸如clsxclassnames之类的库。

正如Vayne在上面的评论中提到的,在你的情况下,你可以创建一个布尔类型的useState来存储你的拖动状态,然后将它作为条件传递给clsxclassnames。由于重新渲染的结果,你的组件总是具有所期望的类。

示例:

import clsx from 'clsx';
import styles from '../css/basic.module.css';

function DragComponents() {
  const [isDragging, setIsDragging] = useState(false)

  const handleStart = (e,data) => {
    setIsDragging(true);
  }
  const handleStop = (e,data) => {
    setIsDragging(false);
  }

  return (
    <Draggable onStart={handleStart} onDrag={handleDrag} onStop={handleStop}>   
      <div className={clsx('handle', {
        'dragging-shelf': isDragging
      })}>
          <MyObj></MyObj>
      </div>
    </Draggable>
  )
}

我希望这可以解决你的问题。

英文:

In ReactJS, whenever you want to make a list of classes dynamically, you can use libraries such as clsx or classnames.

As Vayne mentioned in the comment above, in your case, you can create a boolean useState to store your dragging status, then pass it in clsx or classnames as a condition. As a result of re-rendering, your component always has expected classes.

Example:

import clsx from &#39;clsx&#39;;
import styles from &#39;../css/basic.module.css&#39;;

function DragComponnets() {
  const [isDragging, setIsDragging] = useState(false)

  const handleStart = (e,data) =&gt; {
    setIsDragging(true);
  }
  const handleStop = (e,data) =&gt; {
    setIsDragging(false);
  }

  return (
    &lt;Draggable onStart={handleStart} onDrag={handleDrag} onStop={handleStop}&gt;   
      &lt;div className={clsx(&#39;handle&#39;, {
        &#39;dragging-shelf&#39;: isDragging
      )}&gt;
          &lt;MyObj&gt;&lt;/MyObj&gt;
      &lt;/div&gt;
    &lt;/Draggable&gt;
  )
}

I hope this can solve your problem.

答案2

得分: 0

The imperatively toggled css class is probably removed when the call to setIsDragging causes a re-render. Set the class declaratively instead:

...
return (
  &lt;Draggable className={isDragging ? &#39;dragging-shelf&#39; : &#39;&#39;} ...&gt;   
    ...
  &lt;/Draggable&gt;
)
英文:

The imperatively toggled css class is probably removed when the call to setIsDragging causes a re-render. Set the class declaratively instead:

...
return (
  &lt;Draggable className={isDragging ? &#39;dragging-shelf&#39; : &#39;&#39;} ...&gt;   
    ...
  &lt;/Draggable&gt;
)

huangapple
  • 本文由 发表于 2023年6月8日 16:14:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429884.html
匿名

发表评论

匿名网友

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

确定