javascript - react es6写法时,事件里面拿不到props和state的值
本文介绍了javascript - react es6写法时,事件里面拿不到props和state的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
下面是我的代码:
export default class CouseTest extends Component{
static defaultProps = {
x:1,
y:'4'
}
constructor(props){
super(props);
this.state={
x:0
}
}
onClicks(){
let imgX = this.props.x;//props
let imgTarget = document.getElementsByTagName('img');
let boxTarget = document.getElementsByClassName('moveBox')[0];
let imgWidth = imgTarget[0].offsetWidth;
let imgLength = imgTarget.length;
boxTarget.style.marginLeft = -(this.state.x)+'px';//state
}
render(){
})
在点击事件里面获取 state
和 props
的值时,结果都为空,这个地方要怎么弄呢,求指教
解决方案
构造函数里面:
this.onClicks= this.onClicks.bind(this);
官方文档:https://facebook.github.io/re...
这篇关于javascript - react es6写法时,事件里面拿不到props和state的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文