React: Can't Call Prop Function When It Is Inside Of Another Function? March 31, 2023 Post a Comment I am trying to move over the Auth0 login function as described in their tutorial. I am able to get it work if I use it like this: Solution 1: bind does not call your function: The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called. docs Also, you are setting the value of onClick prop to the return value of login. If you want to pass a reference to the function, you have to do it without the (). Your code should look like this: <button className="btn" onClick={() => this.login()}>test</button> <!-- You need to keep a reference to `this`, hence the binding --> Copy Then: login() { this.props.route.auth.login(); } Copy I edited the answer so that it uses an arrow function. However, I prefer not doing that, since it makes the code a bit cumbersome, and rather bind all the functions in the constructor, like @patrick-w-mcmahon did. Solution 2: Let's say you have a container MyContainer and this container renders a view called MyView. This view has a button that calls a method. MyContainer is going to pass to the MyView the method it needs to use. MyContainer: class MyContainer extends React.Component { constructor(props) { super(props); this.myFunc = this.myFunc.bind(this); } myFunc() { console.log("hello world"); } render() { return <MyView myClick={this.myFunc}/>; } } Copy MyView: const MyView = ({ myClick }) => { return <button onClick={myClick} />; }; MyView.propTypes = { myClick: PropTypes.func }; export default MyView; Copy You pass the needed function from the container to the view and the view calls its parents function from props. the use of bind() sets this scope to the current scope so that when you call this from a different scope it is going to be the scope of the bind. When you are in the render you run a different scope so you must bind your functions to the current class scope so that this.myReallyCoolFunction() is pointing to the correct scope (your class scope). Solution 3: .bind() will only bind the object and arguments but won't call (run) the function. TL;DR Just use .call() instead of .bind() instead of .bind() you can use .call(this, args) which is basicaly the same as bind only that call will call (run) the function. you could also use .apply(), which is basicaly the same as .call() but takes an array with the arguments instead of object like .call() this way you can avoid arrow functions in you jsx render() and kind of keeping the line of thought with react. something like -> login() { this.props.route.auth.login.call(this); } Copy When you call props function through return(JSX) React takes care of calling it once propagation ends. Share Post a Comment for "React: Can't Call Prop Function When It Is Inside Of Another Function?"
Post a Comment for "React: Can't Call Prop Function When It Is Inside Of Another Function?"