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

126
Vistas
React error on onclick event and function handling

I have this react code

const MenuItem = ({title, imageUrl, linkUrl}) => {
    const [isClicked, setIsClicked] = useState(false);

    const handleClick = () =>{
        setIsClicked(true);
    }

    const handleLocation = () => {
        if(window.location.pathname === '/movie'){  
            <MoviePage categoryId={linkUrl}/>
        }
    }

    return ( 
            <div className="menu-item">
                <div className="image-container" style={{backgroundImage:`url(${imageUrl})`}}/>
                    <div className="image-content"
                        onClick={handleClick}>
                        {isClicked ? {handleLocation}: null}
                        <h1>{title}</h1>
                </div> 
            </div>
    );
}

Here, on the onclick event I want to render the component on another page, so I used window.location, but getting this error

Uncaught Error: Objects are not valid as a React child (found: object with keys {handleLocation}). If you meant to render a collection of children, use an array instead.

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

0

This is not how you would navigate in a React app - you need a routing solution, for example https://reactrouter.com/

With react-router you can define and configure your routes and navigate to them without reloading the page.

Try to work you through the tutorial: https://reactrouter.com/docs/en/v6/getting-started/tutorial

Basically, you would have a router with routes like this:

 <BrowserRouter>
    <Routes>
      <Route path="/" element={<App />} />
      <Route path="expenses" element={<Expenses />} />
      <Route path="invoices" element={<Invoices />} />
    </Routes>
  </BrowserRouter>
about 4 years ago · Juan Pablo Isaza Denunciar

0

From offical document,

You can put any valid JavaScript expression inside the curly braces in JSX.

JSX is an Expression Too After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.

<div
  className="image-content"
  onClick={handleClick}
>
    {isClicked ? {handleLocation} : null} // should be {isClicked ? handleLocation() : null}
    <h1>{title}</h1>
</div> 

In your code, handleLocation with curly braces will be the Javascript expression by JSX grammer. So {handleLocation} will be a reference of your function handleLocation. It is never called. You should make it called like handleLocation().

const handleLocation = () => {
  if (window.location.pathname === '/movie') {  
    <MoviePage categoryId={linkUrl}/> // it is just a expression, not returns any value
  }
}

One more change you need in your code is to return JSX element in handleLocation. <MoviePage /> in the if statement is just a expression. So this function whill never return any value. You should add return in front of <MoviePage />, then ReactDOM will render this component.

return <MoviePage categoryId={linkUrl} />
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