簡體   English   中英

如何訪問BaseComponent的ref或任何級別的組件?

[英]How can I access the ref of the BaseComponent or any level of the components?

原始問題: https//github.com/acdlite/recompose/issues/232

如何訪問BaseComponent的ref或任何級別的組件?

碼:

class BaseComponent {
    doSth(){...}
}
const Component compose(...some)(BaseComponent);

class App {
    componentDidMount() {
        // undefined(doSth) is not a function
        this.refs.component.doSth();
    }
    render() {
        <Component ref="component" />
   }
}

你可以使用hoistStatic

class BaseComponent {
  doSth(){...}
}

const enhance = compose(...some)

const Component = hoistStatics(enhance)(BaseComponent)

class App {
  componentDidMount() {
    this.refs.component.doSth()
  }
  render() {
    <Component ref="component" />
  }
}

你可以在測試中找到一個真實的例子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM