How to access a DOM element in React? What is the equilvalent of document.getElementById() in React

前端 未结 4 1841
面向向阳花
面向向阳花 2020-11-22 05:34

How do I select certain bars in react.js?

This is my code:

var Progressbar = React.createClass({
    getInitialState: function () {
        return {          


        
4条回答
  •  爱一瞬间的悲伤
    2020-11-22 06:29

    Since React uses JSX code to create an HTML we cannot refer dom using regulation methods like documment.querySelector or getElementById.

    Instead we can use React ref system to access and manipulate Dom as shown in below example:

    constructor(props){
    
        super(props);
        this.imageRef = React.createRef(); // create react ref
    }
    
    componentDidMount(){
    
        **console.log(this.imageRef)** // acessing the attributes of img tag when dom loads
    }
    
    
    render = (props) => {
    
    const {urls,description} = this.props.image;
        return (
    
                {description}
    
            );
    
    }
    

    }

提交回复
热议问题