I want to use ref attribute on a conditional span element that generates based on reactJs props. this span element is generated as a jsx string and I want to add ref attribute to this span element so that I can use this to reference the click event. since this is conditional if I try to define this in the componentDidMount function, it renders me undefined, as this.props.someConditio
needs to be true in order for that element to be created. hence
let node = React.createRef();
did not work for me.
code:
render() {
return <div>{this.props.someCondition && this.renderMySpanElement.bind()}</div>
}
renderMySpanElement() {
let somestyle ="border:none;"
let myspan = "viewspan";
return `<span ref=${myspan}style=${somestyle}>My span element</span>`;
}
I see this generated in the DOM, however when I access : this.refs.viewspan
or this.viewspan
it renders undefined. any idea how to define the attribute ref on a JSX string?
thx
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…