ref: 獲取組件實體的參考,即獲取實體的this,
影響原因
- 當一個組件用了修飾器之后,其ref會被包裹一層,而不能找到真實的this實體,
- ref在react組件中,同為關鍵字,子組件取不到該props
解決方法
- 在組件外層包裹一層 container,手動系結ref 指定getInstance方法為獲取其ref方法
function withRef(Wrap) {
return class extends Component {
constructor(props) {
super(props);
}
getRefs = o => {
const { getInstance } = this.props;
if (getInstance) {
getInstance(o)
}
}
test2 = () => 222
render() {
return <Wrap {...this.props} ref={this.getRefs} />
}
}
}
- 針對 react-redux 的 connect 高階組件
- v6.0 版本以下可用上述第一個方法對其包裹
- v6.0 以上(包括6.0)可添加 forwardRef 引數?

react-redux 官方檔案 https://react-redux.js.org/api/connect
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/141693.html
標籤:JavaScript
上一篇:高性能的小程式swiper組件
下一篇:JavaScript 基本常識
