Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

100
Vistas
Accessing an object with dot notation using props

I'm trying to use React-DnD and I have this code:

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

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.

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?

Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I geuss you look for: ItemTypes[prop.type]

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda