Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

usage of ref attribute on HTML string - ReactJs/Javascript

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...