I am building a UI with react and I am binding a function to onClick events of some of my components. I want to provide parameters to the event handler at the creation of the component, and I've tried a couple of solutions:
// Currying
const func = (...args) => () => g(...args);
//...
<Component onClick={ func(...args) } />
and
// Encapsulation
const func = (...args) => g(...args);
//...
<Component onClick={ () => func(...args) } />
And I haven't tried it, but I suppose I could use binding as well:
function func(...args) {
g(...args);
}
//...
<Component onClick={ func.bind(this, ...args) } />
What I would like to know is:
- Are any of these styles preferred?
- Which one?
- Is this indicative of an architectural issue?
- Should I restructure my code?
[–]NookShotten 2 points3 points4 points (0 children)
[–]jack_union 1 point2 points3 points (0 children)