Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSX props should not use .bind()

How to fix this error when I have the binding this way: previously binding in constructor solved but this is a bit complex for me:

{this.onClick.bind(this, 'someString')}>

and

<form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))}>
like image 580
monkeyjs Avatar asked Nov 23 '16 20:11

monkeyjs


People also ask

Why shouldn't JSX props use arrow functions or bind?

Using arrow functions or binding in JSX is a bad practice that hurts performance, because the function is recreated on each render. Whenever a function is created, the previous function is garbage collected. Rerendering many elements might create jank in animations.

Is Binding necessary React?

With React, typically you only need to bind the methods you pass to other components. For example, <button onClick={this.handleClick}> passes this.handleClick so you want to bind it. However, it is unnecessary to bind the render method or the lifecycle methods: we don't pass them to other components.

Do we need to bind arrow function in React?

It does not re-scope this, so we don't need to bind this in the class constructor. JavaScript has first-class functions, which means functions are considered as data. Therefore, arrow functions can be assigned to class properties.

Why is bind USED IN React?

The bind() is an inbuilt method in React that is used to pass the data as an argument to the function of a class based component.


2 Answers

Option 1:

Use arrow functions (with babel-plugins) PS:- Experimental feature

class MyComponent extends Component {
   handleClick = (args) => () => {
      // access args here;
      // handle the click event
   }

   render() {
     return (
       <div onClick={this.handleClick(args)}>
         .....
       </div>
     )
   }
 }

Option 2: Not recommended

Define arrow functions in render

   class MyComponent extends Component {
       render() {
         const handleClick = () => {
          // handle the click event
         }
         return (
           <div onClick={handleClick}>
             .....
           </div>
         )
       }
     }

Option 3:

Use binding in constructor

   class MyComponent extends Component {
       constructor() {
         super();
         this.handleClick = this.handleClick.bind(this);
       }

       handleClick() {
          // handle click
       }

       render() {

         return (
           <div onClick={this.handleClick}>
             .....
           </div>
         )
       }
     }
like image 74
anoop Avatar answered Oct 04 '22 19:10

anoop


I recommend you to use binding in the class constructor. This will avoid inline repetition (and confusion) and will execute the "bind" only once (when component is initiated).

Here's an example how you can achieve cleaner JSX in your use-case:

class YourComponent extends React.Component {
    constructor(props) {
        super(props);

        // Bind functions
        this.handleClick = this.handleClick.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    handleClick() {
        this.onClick('someString');
    }
    onClick(param) {
        // That's your 'onClick' function
        // param = 'someString'
    }

    handleSubmit() {
        // Same here.
        this.handleFormSubmit();
    }
    handleFormSubmit() {
        // That's your 'handleFormSubmit' function
    }

    render() {
        return (
            <form onSubmit={this.handleSubmit}>
                <p>...</p>
                <button onClick={this.handleClick} type="button">Cancel</button>
                <button type="submit">Go!</button>
            </form>
        );
    }
}
like image 32
Kaloyan Kosev Avatar answered Oct 04 '22 19:10

Kaloyan Kosev