React refs未被评估

时间:2017-11-27 17:23:58

标签: reactjs

我在React中有一个简单的组件,女巫渲染另一个组件如下。

   _getResultsListComponent: function () {
      var data = {
        ...blabla
      };
      return 
          <Popout
            id='popout'
            ref="searchResults"
            closeResults={this._closeResults}
            pointerAlign="center"
         >
            <this.props.resultsList data={data} />
         </Popout>
   },


           render: function () {
                    return (
         <div className='blabla'>
            {this._getResultsListComponent()}
         </div>
      );
   }

但是,如果我在mount ill之后打印'this.refs',请输入精确的字符串ive,例如:

 ref="searchResults"

在mount之后打印this.refs

searchResults

如果我将参考更改为

 ref="{(compo) => {this.component = compo}}"

打印时生病:

{(compo) => {this.component = compo}}

就像没有评估裁判一样。

我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

React不再设置&#34; refs&#34;类上的对象,但使用回调,以便您可以为该元素分配一个唯一的名称。在这种情况下,this.component将包含传递给回调的compo道具的值。

ref接受callback函数,您需要删除要评估的引号

ref={(compo) => {this.component = compo}}

您稍后可以像this.component

那样访问参考号