question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react/jsx-no-bind: how to pass args from callbacks?

See original GitHub issue

I have a question about react/jsx-no-bind rule in 3.0.0: how can I pass args to handler from callback if I can’t use bind and arrow functions?

doSomething(thing) { ... }

render() {
  const { items } = this.props;

  return (
    <ul>
    {
      items.map(item => (
        <li onClick={this.doSomething.bind(this, item)} /> // <--- JSX props should not use .bind()
        <li onClick={() => this.doSomething(item)} />      // <--- JSX props should not use arrow functions
      ))
    }
    </ul>
  );
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:85
  • Comments:76 (2 by maintainers)

github_iconTop GitHub Comments

70reactions
geekjuicecommented, Jan 6, 2016

If you still want to do it inline, it’s possible to use something like to _.partial or

function partial(fn, ...args) {
  return fn.bind(fn, ...args);
}

React.createClass({
  handler(item) {
    // do something
  },

  render() {
    const {items} = this.props;
    return items.map(item => {
      return <div onClick={partial(this.handler, item)} />;
    });
  }
});

This will pass through the linter, but will still have the issues listed in eslint-plugin-react#jsx-no-bind documentation.

45reactions
ljharbcommented, Jan 6, 2016

Right - at that point you might as well disable the rule.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React: how to pass arguments to the callback - Stack Overflow
What you can do is create a partially applied or higher order function to enclose the item.id and pass it along.
Read more >
Passing function to components, Callbacks in React, context ...
First things first, if you are new to react, you can have a look at official documentation of passing functions to components here....
Read more >
Flexible Callback Signatures | Dash for Python Documentation
Callback functions can register to be called with named keyword arguments. You can do this by passing dictionaries to the inputs and state...
Read more >
How can I pass arguments to Tkinter button's callback ...
We will pass the label as the argument in the button command by using lambda function. #Import necessary Library from tkinter import *...
Read more >
How to pass a parameter to an event handler or callback
This will help you to understand the underlying logic of event handlers and passing parameters through it. Creating React Project: Step 1: To ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found