最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - losing react component ref after I Wrapped it by injectIntl - Stack Overflow

programmeradmin3浏览0评论

I have an issue with getting the ref to the func in React ponents after I am wrapping it with injectIntl. basically what I need is to get access to a func in the ponent by ref here is what I am doing

class MainContainer extends React.Component {
    constructor(props) {
        super(props);
        }

    getSamples(){
      return sth
    }

   render() {
        return (<div>this.props.sth</div>)
        }

export default injectIntl(MainContainer )

it possible to get the ref to the MainContainer after wrapped it with injectIntl?

I have an issue with getting the ref to the func in React ponents after I am wrapping it with injectIntl. basically what I need is to get access to a func in the ponent by ref here is what I am doing

class MainContainer extends React.Component {
    constructor(props) {
        super(props);
        }

    getSamples(){
      return sth
    }

   render() {
        return (<div>this.props.sth</div>)
        }

export default injectIntl(MainContainer )

it possible to get the ref to the MainContainer after wrapped it with injectIntl?

Share Improve this question asked Jun 28, 2017 at 14:51 Saif AlradhiSaif Alradhi 1293 silver badges15 bronze badges 1
  • Are you trying to call a method of MainContainer? Your calling code would helpful – Björn Heiß Commented Jun 28, 2017 at 15:04
Add a ment  | 

2 Answers 2

Reset to default 5

The withRef option should be passed.

export default injectIntl(MainContainer,{ withRef: true })

The MainContainer wrapper ponent instance can be retrieved using

<MainContainer ref={c => { this.container = c; }} />

The wrapped ponent instance can be retrieved using

this.container.getWrappedInstance();

injectIntl has a forwardRef property which causes it to pass down ref to the wrapped ponent.

// MyComponent.jsx
// ...
export default injectIntl(MyComponent, {forwardRef: true});
// MyApp.js
import MyComponent from 'MyComponent';
class MyApp {
  render() {
    this.myComponentRef = React.createRef();
    return <MyComponent ref={ref} />;
  }
} 

reference

发布评论

评论列表(0)

  1. 暂无评论