亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

react中使用ref報錯

react中使用ref報錯

藍山帝景 2018-09-19 09:09:40
var MyComponent = React.createClass({   handleClick: function() {     this.refs.myTextInput.focus();   },   render: function() {    return (       <div>         <input type="text" ref="myTextInput" />         <input type="button" value="Focus the text input" onClick={this.handleClick} />       </div>     );   } }); ReactDOM.render(   <MyComponent />,   document.body );通過用this.refs.myTextInput.focus();,可是頁面沒有效果,提示錯誤:Error: addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component's `render` method, or you have multiple copies of React loaded  (details: https://fb.me/react-refs-must-have-owner).求指導,謝謝
查看完整描述

1 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

親測沒有問題;
這類的報錯一般出現在沒有使用render的component里面,比如下面這樣:

const Comp = ({test}) => {    return (        <div ref="error">assdfas</div>
    )
}

你這里的寫法是沒有問題,接下來需要確認一下你的react的版本,也就是這個原因 ‘you have multiple copies of React loaded’


查看完整回答
反對 回復 2018-10-08
  • 1 回答
  • 0 關注
  • 1628 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號