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

178
Vistas
How to pass child prop to parent?

I've got some dynamic buttons that are a child component, and get assigned a value="URL" based off of my MongoDB. How do I go about passing that generated value to my parent/web component src={currentSrc}? When I assign it, it says that currentSrc is not defined?

Here's the SizeOptions:

import { useState } from "react";

export const SizeOptions = ({ size }) => {
  const sizeName = Object.keys(size);

  // Update Model Viewers Src
  function changeSize (){
    setSrc(currentSrc)
    console.log(currentSrc)
  }

  if (!sizeName) return <></>;

  return (
    <div>
        {size[sizeName].map((item) => (
          <button key={item} className='size' value={item} onClick={changeSize} currentSrc={item}>
            {sizeName}
          </button>
        ))}
    </div>
  );
};

And here is the ProductScreen:

import './ProductScreen.css';
import { useEffect, useState } from "react";

//Components
import { SizeOptions } from '../components/SizeOptions';

const [currentSrc, setSrc] = useState(size[sizeName][0])

const ProductScreen = ({match}) => {
return(
<div className='sizebuttons'>
  {product && (product.size || []).map((size, index) => (<SizeOptions  key={index} size={size} setSrc={currentSrc} changeSize={changeSize}/>))}
</div>
<div className="productscreen__right">
  <model-viewer
    id="model-viewer"
    src={currentSrc}
    alt={product.productName}
    ar
    ar-modes="scene-viewer quick-look"
    ar-placement="floor"
    shadow-intensity="1"
    camera-controls
    min-camera-orbit={product.mincameraorbit} 
    max-camera-orbit={product.maxcameraorbit}
    interaction-prompt="none">  
      <button slot="ar-button" className="ar-button">
        View in your space
      </button>
  </model-viewer>
</div>
)}

The rendered button:

HTML

I have more code inside my ProductScreen, I just tried to keep it as minimized as possible to try and make it easier to help me figure it out! Any help would be greatly appreciated!

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

0

Good news! I finally figured it out after many, many attempts.

So in order to pass from child to parent this is what I came up with:

SizeOptions.js

export const SizeOptions = ({ size, changeSrc }) => {
    const sizeName = Object.keys(size);

    if (!sizeName) return <></>;

    return (
    <div>
        {size[sizeName].map((url) => (
            <button key={url} className='size' value={url} onClick={() => changeSrc(url)}>
                {sizeName}
            </button>
        ))}
    </div>
  );
};

export default SizeOptions;

ProductScreen.js

import './ProductScreen.css';
import { useEffect, useState } from "react";
import { useSelector, useDispatch } from "react-redux";

// Actions
import { getProductDetails } from "../redux/actions/productActions";

//Components
import { SizeOptions } from '../components/SizeOptions';

const ProductScreen = ({match}) => {

    const dispatch = useDispatch();
    
    const productDetails = useSelector(state => state.getProductDetails);
    const { loading, error, product } = productDetails;
    
    const [ src, setSrc ] = useState("Default URL")

return(

    <div className='sizebuttons'>
        {product && (product.size || []).map((size, index) => (<SizeOptions  key={index} size=  {size} changeSrc={src => setSrc(src)}/>))}
    </div>
              
    <model-viewer
        id="model-viewer"
        src={src}
        alt={product.productName}
        ar
        ar-modes="scene-viewer quick-look"
        ar-placement="floor"
        shadow-intensity="1"
        camera-controls
        min-camera-orbit={product.mincameraorbit} 
        max-camera-orbit={product.maxcameraorbit}
        interaction-prompt="none">  
        <button slot="ar-button" className="ar-button">
            View in your space
        </button>
    </model-viewer>
)};

The only issue now is how to make the "Default URL" actually be a URL as right now if I put {src} there, it says it cannot do that before the component is rendered.

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