You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A simple component may have public methods that are accessible if it's a ref somewhere. I usually use if for focus, scrollToBottom etc.
classArticleSearchextendsComponent{render(){return<div><inputtype="text"ref="input"/></div>}focus(){this.refs.input.focus()}}// some other component:render(){return<div>...<ArticleSearchref="articleSearch"/>...</div>}doSomething(){this.refs.articleSearch.focus()}
In case component is @connected, the methods aren't accessible anymore.
I can use withRef: true and this.refs.articleSearch.getWrappedInstance().focus() but that wouldn't be clean, as I explicitly specify the getWrappedInstance(). If I remove the @connect code will break.
Would it make sense to copy some selective methods from wrapped component into the Connect component?
functionconnect(mapStateToProps,mapDispatchToProps,mergeProps,options={}){// ...returnfunctionwrapWithConnect(WrappedComponent){classConnectextendsComponent{// ...constructor(props,context){//...if(options.copyMethods){invariant(options.withRef,'copyMethods requires the withRef option to be true')options.copyMethods.forEach(m=>this[m]=(...args){returnthis.wrappedInstance[m](...args)})}}
I see all static methods are copied with hoistStatics, but prototype methods are left out. Solution can be something similar that also copies all prototype methods and binds them to the ref.
The text was updated successfully, but these errors were encountered:
A simple component may have public methods that are accessible if it's a ref somewhere. I usually use if for
focus
,scrollToBottom
etc.In case component is
@connect
ed, the methods aren't accessible anymore.I can use
withRef: true
andthis.refs.articleSearch.getWrappedInstance().focus()
but that wouldn't be clean, as I explicitly specify thegetWrappedInstance()
. If I remove the@connect
code will break.Would it make sense to copy some selective methods from wrapped component into the
Connect
component?Something like (https://github.com/rackt/react-redux/blob/master/src%2Fcomponents%2Fconnect.js) :Usage:I'll make a PR if it seems fine.
Update
I see all static methods are copied with
hoistStatics
, but prototype methods are left out. Solution can be something similar that also copies all prototype methods and binds them to the ref.The text was updated successfully, but these errors were encountered: