Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to pass a function with params to a react component #101

Open
phliem opened this issue Mar 26, 2019 · 4 comments
Open

Is there a way to pass a function with params to a react component #101

phliem opened this issue Mar 26, 2019 · 4 comments

Comments

@phliem
Copy link

phliem commented Mar 26, 2019

If I have a parent angular component and I want to pass a function without any parameters to a react child component, this will work
Screenshot 2019-03-26 at 10 28 32

The problem is that I need to pass some parameter in the chooseSolution function. This will not work as the chooseSolution function is immediately invoked and I will have an infinite loop error
Screenshot 2019-03-26 at 11 09 11

And here is the error
Screenshot 2019-03-26 at 11 18 35

What I don't really understand is the reason why the function chooseSolution is invoked as soon as I pass a parameter from an angular parent component to a react child component.

I have recreate the problem here (Angular Parent and React Child)
https://stackblitz.com/edit/react2angular-fkbnpd?file=angular-component.js

If I recreated the same example in an Angular Parent and Angular Child context, it will work without immediately invoking my function chooseSolution. Here is an working example (Angular Parent and Angular Child)
https://stackblitz.com/edit/react2angular-my4bmr?file=angular-component.js

Note that I cannot create two functions like the following, because the number of buttons is a dynamic value. For example i will have 10 different buttons, so can't really create 10 different functions dynamically
Screenshot 2019-03-26 at 11 25 04

@nnals
Copy link

nnals commented Apr 3, 2019

hello, i just found my way here through a google search and have never used this library, so this might not work, but have you tried passing in in an arrow function?

() => $ctrl.chooseSolution('SOLUTION_A’)

@phliem
Copy link
Author

phliem commented Apr 4, 2019

hello, i just found my way here through a google search and have never used this library, so this might not work, but have you tried passing in in an arrow function?

() => $ctrl.chooseSolution('SOLUTION_A’)

Hi nnals, thanks for your help! I already tried the arrow function without success previously. It endup in a syntax error

@tristanHessell
Copy link

The reason the function chooseSolution is invoked is because the angular component created with react2angular binds via one-way bindings (<) rather than callback bindings (&).

A possible solution is to define chooseSolution to be (solution) => () => { /*do whatever you actually want to do*/ };.

(Apologies for any poor formatting - on my phone).

@kanapka94
Copy link

kanapka94 commented Jan 30, 2020

@phliem try this solution: #43 (comment)

In controller should create arrow function:

tip: solutionName is invented variable name

chooseSolution = solutionName => { /* something */ }

And to correct parse this code you should install babel plugin @babel/plugin-proposal-class-properties

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants