[英]Accessing object properties outside of class or object using dot notation?
[英]Accessing an object with dot notation using props
我正在尝试使用 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;
但这条线
type: ItemTypes.{props.type}
不起作用。
常量.tsx
export const ItemTypes = {
raspuns1: 'raspuns1',
raspuns2: 'raspuns2',
raspuns3: 'raspuns3',
raspuns4: 'raspuns4'
}
在父组件中,我想决定将这 4 个选项中的哪一个传递给子组件,然后通过道具 raspuns1/raspuns2/raspuns3/raspuns4 发送。
在上面的代码中,您可以看到我的方法 - 我也尝试过使用括号表示法,但它也没有奏效。
是否可以通过这种方式访问对象 ItemTypes 中的数据?
谢谢。
我猜你在找: ItemTypes[prop.type]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.