我不确定如何为功能组件实现引用。我知道您需要使用forwardRef,但是我不明白如何在代码的其他地方指向它们。例如,我正在渲染<img ref="someStr">
,然后通过this.refs.someStr在componentDidMount中引用该元素。
这是我的原始代码:
componentDidUpdate(prevProps, prevState) {
if (this.props.activeMarker !== prevProps.activeMarker) {
let img_ref = this.refs.image
if (this.props.showingInfoWindow) {
img_ref.style.border = "2px solid black"
}
else {
img_ref.style.border = null
}
}
}
return ( <img src={this.props.image} alt="unit" ref="cg_image" style={{width:200, height:100}} />)
我想用reactstrap组件替换img:
const fancyCard = React.forwardRef((props,ref) => (
<CardImg ref={ref} top width="100%" src={this.props.image}/>
))
在componentDidMount中引用非字符串refs值的类似方法是什么?