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

104
Vistas
How to pass a function to a component using arrow function?

FYI: I am using ES6 on ReactJS

I have a switcher. I need it to switch to the other side when clicked. If click on the side that is currently active does nothing.

Here is my sample code

import { useState } from 'react'
const {isLeftButton, setIsLeftButton} = useState(true);
const toggleSwitcher = () => {
    setIsLeftButton(!isLeftButton)
}

const home = () => {
...
    return (
        <CustomSwitcher isLeftButton={isLeftButton} toggleSwitcher={toggleSwitcher} />
    )
...
}
export default Home

Here is the code inside the CustomSwitcher

const CustomSwitcher = (isLeftButton, toggleSwitcher) => {

    const leftButton = () => {
        if (isLeftButton !== true) {
            toggleSwitcher()
        }
    }

    const rightButton = (isLeftButton, toggleSwitcher) => {
        if (isRightButton === true) {
            toggleSwitcher()
        }
    }

    return (
         <div>
             <CustomButton onClick={LeftButton}>Left Button</CustomButton>
             <CustomButton onClick={rightButton }>Right Button</CustomButton>
         </div>
        )
    }
export default CustomSwitcher

However I got this error

TypeError: toggleSwitcheris not a function
  12 |    const CustomSwitcher = () => {
  13 |        
  14 |        if (leftButton !== true) {
> 15 |            toggleSwitcher()
     | ^  16 |        }
  17 |    }
  18 | 

As I understand, when passing a function down to a component. The function is no longer a function.

And I don't think my code is good. If you can think of a better way to do so. Please contribute.

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

0

You are not using the correct way to access the props.

Try to replace

const CustomSwitcher = (isLeftButton, toggleSwitcher) => {

with

const CustomSwitcher = ({isLeftButton, toggleSwitcher}) => {
about 4 years ago · Juan Pablo Isaza Denunciar

0

const CustomSwitcher = (isLeftButton, toggleSwitcher) => { ... }

is not the correct way to build a component.

Either use the props object

const CustomSwitcher = (props) => {
    // props.toggleSwitcher
}

Or destructure props

cost CustomSwitcher = ({isLeftButton, toggleSwitcher}) => { ... }
about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to use useState inside a functional component. In your case, inside home. Hooks cannot be used at the global level. Consequently, you need to define toggleSwitcher inside home also.

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