Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!