1

I'm trying to wrap my head around functional components and binding.

If I have a functional React component that looks like this:

const Dashboard = props => {
    const _dashboard = new _Dashboard()

    return (
        <div>
            <div className="title">Dashboard</div>
            <button>Log</button>
        </div>
    )
}

And a class that looks like this:

class _Dashboard {
    constructor() { }

    log() {
        console.log('Dashboard Log Clicked!')
    }
}

What is the proper way to bind the button click?

This is what I tried and doesn't work

const Dashboard = props => {
    const _dashboard = new _Dashboard()

    return (
        <div>
            <div className="title">Dashboard</div>
            <button onClick={ _dashboard.log }>Log</button>
        </div>
    )
}

but I feel like I'm missing something in the general way it works.

Edit: I removed the parenthesis from the log() call. Neither way is writing anything to the console.

6
  • could you check the value of your _dashboard in dev tools? (line: <button onClick={ _dashboard.log }>Log</button> ) Commented Mar 1, 2016 at 0:57
  • It doesn't even look like _dashboard is getting instantiated. adding console.log('test') to the _Dashboard class is never getting called Commented Mar 1, 2016 at 1:01
  • try this._dashboard? also the console.log('test'); should IMO be on the componentWillMount() or componentDidMount() of the class if you want to see it. Are you getting any console errors btw? Commented Mar 1, 2016 at 1:08
  • btw; try replacing your line: "<button onClick={ _dashboard.log }>Log</button>" with "<button onClick={ () => { console.log('test'); } }>Log</button>" . If that doesn't run, then your problem is a different one than binding the button onClick event correctly. Commented Mar 1, 2016 at 1:13
  • Ok, so looks like something else is going on, because <button onClick={ () => { console.log('test'); } }>Log</button> didn't do anything either. Back to the drawing board. Haha. The weirdest thing is there are no errors. Everything renders fine. Commented Mar 1, 2016 at 1:33

3 Answers 3

1

you are calling the function aka _dashboard.log() instead of _dashboard.log ;

Sign up to request clarification or add additional context in comments.

1 Comment

& if you need to bind it to the button, you'd write ::_dashboard.log; or _dashboard.log.bind(this); (supposing for instance you need the value associated with that button).
0

You just need to remove the parenthesis otherwise you are actually invoking the function and binding onClick to the result of that function. Without parenthesis you pass a reference to the function.

<button onClick={_dashboard.log}>Log</button>

Comments

0

you have to write something like:

 <button onClick={_dashboard.log}>Log</button>

or

 <button onClick={() => _dashboard.log()}>Log</button>

otherwise your code will be executed immediatly and not bound.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.