javascript - 使用 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 个选项中的哪一个传递给子组件,然后通过 props raspuns1/raspuns2/raspuns3/raspuns4 发送。
在上面的代码中,您可以看到我的方法 - 我也尝试使用括号表示法,但它也没有奏效。
是否可以通过这种方式访问对象 ItemTypes 中的数据?
谢谢。
解决方案
我猜你在找:ItemTypes[prop.type]
推荐阅读
- postgresql - 用户插补时插入语句不起作用
- python - Python如何使用绝对导入
- linux - 有没有办法通过命令行从网页转储加载的脚本?
- python - keras.Model 和 keras.engine.training.Model 有什么区别?
- php - 如何使用 PHP 在 Twilio 中传递自定义参数?
- android - 如何在 Android 中实现类似 Page/Form sheet 的 iOS 13 新卡片?
- javascript - 如何在 react-native 中过滤 JavaScript 对象
- mysql - 如何更新每列中具有不同值的多行
- amazon-web-services - 如何使用数据管道中的实例队列旋转 EMR 集群?
- c# - 如何在 foreach 循环中将 JsonConvert jsonString 映射到 jsonObject?