[英]Accessing an object with dot notation using props
I'm trying to use React-DnD and I have this code:我正在尝试使用 React-DnD 并且我有以下代码:
import { useDrag } from "react-dnd";
import { ItemTypes } from "./Constants";
type DraggableGameAnswerProps = {
type: string
}
function DraggableGameAnswer(props: DraggableGameAnswerProps)
{
const [{ isDragging }, drag] = useDrag(() => ({
type: ItemTypes.{props.type},
collect: (monitor) => ({
isDragging: !!monitor.isDragging()
})
}))
}
export default DraggableGameAnswer;
but the line但这条线
type: ItemTypes.{props.type}
doesn't work.不起作用。
Constants.tsx常量.tsx
export const ItemTypes = {
raspuns1: 'raspuns1',
raspuns2: 'raspuns2',
raspuns3: 'raspuns3',
raspuns4: 'raspuns4'
}
In the parent component, I want to decide which of those 4 options I want to pass onto the children, and then send through the props raspuns1/raspuns2/raspuns3/raspuns4.在父组件中,我想决定将这 4 个选项中的哪一个传递给子组件,然后通过道具 raspuns1/raspuns2/raspuns3/raspuns4 发送。
In the above code you can see my approach - I also tried with bracket notation, but it hasn't worked either.在上面的代码中,您可以看到我的方法 - 我也尝试过使用括号表示法,但它也没有奏效。
Is it possible to access the data in the object ItemTypes this way?是否可以通过这种方式访问对象 ItemTypes 中的数据?
Thanks.谢谢。
我猜你在找: ItemTypes[prop.type]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.