英文:
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 "../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>
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
,每当你想要动态创建一组类时,你可以使用诸如clsx
或classnames
之类的库。
正如Vayne在上面的评论中提到的,在你的情况下,你可以创建一个布尔类型的useState来存储你的拖动状态,然后将它作为条件传递给clsx
或classnames
。由于重新渲染的结果,你的组件总是具有所期望的类。
示例:
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 'clsx';
import styles from '../css/basic.module.css';
function DragComponnets() {
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>
)
}
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 (
<Draggable className={isDragging ? 'dragging-shelf' : ''} ...>
...
</Draggable>
)
英文:
The imperatively toggled css class is probably removed when the call to setIsDragging
causes a re-render. Set the class declaratively instead:
...
return (
<Draggable className={isDragging ? 'dragging-shelf' : ''} ...>
...
</Draggable>
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论