question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Decorator shields public methods of a class

See original GitHub issue

After decorating a dropdown:

@withStyles(style)
export default class UiDropdown

I’m no longer able to reference it:

this.dropdownRef.show() results in TypeError: this.dropdownRef.show is not a function

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:34 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
droganovcommented, Mar 14, 2017

@ljharb let me repeat it once again 😃 it fails in both cases

<UiDropdown ref={...} />ref points to withStyles HOC when I expect it to point to the wrapped component i.e. UiDropdown.

And if I look here, I’m not able how find any ref delegation https://github.com/airbnb/react-with-styles/blob/master/src/withStyles.jsx#L45

I suggest it can be solved this way:

Example: In the following example we explore how to access instance methods and the instance itself of the WrappedComponent via refs

function refsHOC(WrappedComponent) {
  return class RefsHOC extends React.Component {
    proc(wrappedComponentInstance) {
      wrappedComponentInstance.method()
    }
    
    render() {
      const props = Object.assign({}, this.props, {ref: this.proc.bind(this)})
      return <WrappedComponent {...props}/>
    }
  }
}
1reaction
ljharbcommented, Mar 15, 2017

The only mechanism for communication is props. ref is a special prop that violates encapsulation. If you use literally any other prop name besides “key” or “ref”, I would expect what you want to work just fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can a decorator of an instance method access the class?
The method decorator marks the method as one that is of interest by adding a "use_class" attribute - functions and methods are also...
Read more >
4. Decorator Pattern - ActionScript 3.0 Design Patterns [Book]
The public method inherited from the Decorator class must be set up using the override statement. ... and the combatants have different weapons...
Read more >
Using the Decorator pattern to add public methods to an object
The decorator pattern prevents the explosion by wrapping around the base window class' existing public methods. The decorator pattern ideally ...
Read more >
Decorating Methods defined in a Class With Python
To decorate a method in a class, first use the '@' symbol followed by the name of the decorator function. A decorator is...
Read more >
Solved We have previously written decorators for ​Animal
Question: We have previously written decorators for ​Animal and ​Comparator classes, but the decorator pattern works for any class hierarchy, although the more ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found