-
Notifications
You must be signed in to change notification settings - Fork 98
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
Decorator shields public methods of a class #53
Comments
withStyles is not a decorator - class decorators take (target, name, description) arguments. withStyles takes (target, options) arguments. You need to |
@ljharb withStyles takes thunk, options, not target (component)? and returns mustbe a decorator If I do
The component gets decorated, but decorator does not let the component instance to be referenced. |
You're correct about the arguments :-) but you can't use a non-decorator as a decorator, and withStyles both is not, and does not return, a decorator. |
If you can provide a non-decorator repro case I'd be happy to reopen this. |
@ljharb the case is that there can be a need to access methods of decorated component from parent component. Here is the example, where button needs to open a dropdown, and if we decorate dropdown this stops working:
|
@ljharb I was able to solve this with custom ref prop:
|
Glad you were able to solve it - but again, anywhere you've done |
@ljharb the code I wrote works as a decorator, more than that you have a readme case for decoration: https://github.com/airbnb/react-with-styles#example-usage But anyway, my current solution is a hack. I've found an article with explanation on how to setup the HOC ref: Please see under "Accessing the instance via Refs" |
It's true, we have that in the documentation. Perhaps we should remove that for now, or modify this to be a proper decorator. @droganov would you be interested in opening a PR for this? |
@lencioni this is not about decoration actually, I'm interested in having access to wrapped component and I even provided a reference :-) |
I guess what I'm saying is one of two things:
@droganov if you use |
@ljharb I can't access wrapped component regardless of wrapping method:
|
Thanks - can you provide the entire source of UiDropdown? |
@ljharb sure!
|
Where's the line that errors? ( |
@ljharb it's in
|
Sorry to repeat over and over again, but this happens even when UiButton does not use withStyles as a decorator? (in other words, |
@ljharb let me repeat it once again :-) it fails in both cases
And if I look here, I'm not able how find any ref delegation I suggest it can be solved this way:
|
ahhhh i see what you mean. the What you want to do instead is make a special prop, say, In fact, I believe your proposed solution won't work because React itself filter out the |
@ljharb yes, it's about ref, but I used function ref, not string. I've forked the package and will try to setup proc on weekend. I will suggest a PR if succeed |
That's better :-) but an explicit prop to provide the ref is better than using the |
I think it should proxy method calls, not ref itself |
so ref will still point to the HOC, but HOC.method() will go down to wrappedComponent.method() |
Instance methods of react components should only be called from within that component. Calling it in a ref callback isn't a great idea. |
Why? React allows public methods. It's up to developer which religion to follow :-) |
In my case it is parent-to-child communication, it can be done directly I think |
That violates React's one-way data model. It's certainly up to the developer to follow or ignore best practices and idioms, but that doesn't exempt it from being a foolish decision. |
One-way is from parent to child in this case, it's all about the transport. As a parent you can say something directly to your child, or can write it down on the fridge (props). I prefer to say |
The only mechanism for communication is props. |
Actually yes, if we want to control the state of UI we need to pass data with props. I agree here. But there are a bunch if situations like prototyping, when you don't need to go the full cycle and need to craft something fast. I try to support both approaches. |
Resurrecting old thread... I feel like there are legit use cases for
Moreover, I feel like it is confusing as a user of library.
Can ref support be implemented? |
Use cases notwithstanding, it's a far better design to have explicit ref props if you need them. |
Use case: the wrapped component class encapsulates a canvas DOM element and exposes a public method getSize. How would you access that? As above, ref has legit puposes aside from breaking data binding encapsulation. |
@richburdon the canvas element would of course use ref, but your wrapper would have an explicit prop like “canvasRef” |
After decorating a dropdown:
I'm no longer able to reference it:
this.dropdownRef.show()
results inTypeError: this.dropdownRef.show is not a function
The text was updated successfully, but these errors were encountered: