3

As many know, this.someFunction = this.someFunction.bind(this) can be used in React class components. However, sometimes it's burdensome to create classes for very simple components. So, how can I bind a function to this without creating a class?

asked Feb 14, 2019 at 4:00
2
  • 1
    you can use es6 arrow function. No need to bind this for that Commented Feb 14, 2019 at 4:05
  • 3
    if you have a simple component how/why are you referencing this inside the component? pure functional components are generally for presentation and should have any values they need passed as props, whats the requirement to access this Commented Feb 14, 2019 at 4:22

5 Answers 5

3

use arrow function arrow function

answered Feb 14, 2019 at 4:03

1 Comment

Your answer is correct, but I suggest you elaborate a little bit with an example, which would add some more value for future readers stumbling upon this question.
2

this makes sense in class component because it refers to component instance. It doesn't make sense in functional component because it is either undefined or a global, depending on the environment and how a function was declared.

As for class components, explicit constructor can be omitted if it's not needed, class fields can be used to assign instance properties, including bound methods:

class Foo extends Component {
 foo = this.foo.bind(this);
 foo() { ... }
 ...
}

Which is syntactic sugar for:

class Foo extends Component {
 constructor(props) {
 super(props);
 this.foo = this.foo.bind(this);
 }
 foo() { ... }
 ...
}

Bound prototype methods have several benefits over instance arrow methods.

answered Feb 14, 2019 at 7:14

Comments

1

In React 16.8, you can use hooks for stateful components, essentially allowing you to write everything as a function and eliminating the need to use classes and all the caveats that come with them (this, .bind() etc).

Usage example

Here's an example of using the useState() hook and the useEffect() hook inside a function, along with an arrow function, which is already bound to the component's context:

import React, { useState, useEffect } from 'react';
function LimitedTextarea({ rows, cols, value, limit }) {
 const [content, setContent] = useState(value);
 const setFormattedContent = text => {
 text.length > limit ? setContent(text.slice(0, limit)) : setContent(text);
 };
 useEffect(() => {
 setFormattedContent(content);
 }, []);
 return (
 <div>
 <textarea
 rows={rows}
 cols={cols}
 onChange={event => setFormattedContent(event.target.value)}
 value={content}
 />
 <p>
 {content.length}/{limit}
 </p>
 </div>
 );
}

Short explanation

  • You use useState() to create a state variable content and a method to update that variable, setContent().
  • You create an arrow function setFormattedContent to update the content state variable via the setContent method, which is already bound to context.
  • You use useEffect() to call setFormattedContent on the value of the content state variable.
  • Finally, you return whatever you would have in your render() method in a class component.
answered Feb 14, 2019 at 7:03

Comments

0

Yes arrow function is the solution.

Bind with this,

this.someFunction = this.someFunction.bind(this)

Using arrow function,

someFunction = () => {
 // do something
}
answered Feb 14, 2019 at 4:09

Comments

0

There is no need to bind "this" If you use arrow function. It has simple and cleaner syntax.

someFuncton = () => {
 // whatever write here...
}
answered Feb 14, 2019 at 4:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.