How to get offsetX in react?
習慣沉默
習慣沉默 2017-06-26 10:57:16
0
3
905

1. How to get the offsetX of an element in react?

My idea is to obtain it through this.state.offsetX, but this is indeed null

2. Certain states of a component can be initialized in react, but when I write getInitialState like this, a warning error appears on the console. The prompt is as shown below:

The specific code is as follows

習慣沉默
習慣沉默

reply all(3)
小葫芦

1. es6 writing method. Initializing the default state is done in the constructor

constructor() {
    super();
    this.state = {
    }
}

2. If you want to use this in the event callback function, you need to bind it manually

// 方法1
this.moveElment.bind(this);

// 方法2
moveElement = event => {
}

// 方式3
<p onMouseEnter={() => this.moveElement}></p>
滿天的星座

getInitialState is how it is written in ES5.
In ES6, state initialization should be placed in the constructor.

class Demo extends Component{
    constructor(){
        super(); // 必须先调用super, 后面才能用 this 
        this.state = {}        
    }
}
洪涛

The error is clearly written, only use it

React.createClass()

getInitialState can only be used when , and used when creating using ES6’s class keyword

this.state = {}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template