![](/img/trans.png)
[英]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.