class ImageGridList extends React.Component {
nodeRef = null
say = () => {
console.log(this.nodeRef)
}
render() {
let that = this;
return (
<div style={{height: '100px', width: '100px', border: '1px solid blue' }}
onClick={this.say} ref={ node => this.nodeRef = node }
/>
)
}
when you click, this.nodeRef is undefined. The 'this' object in say function not equal render function's this.
onClick={this.say.bind(this)} ref={ node => this.nodeRef = node }
will be ok!
constructor(props){
super(props)
this.say = this.say.bind(this)
}
undefined also. the .babelrc file is
{
"presets": [
"react",
["env", {
"modules": false,
"targets": {
"browsers": [
"last 1 Chrome versions"
],
"node": "current"
}
}]
],
"plugins": [
"react-hot-loader/babel",
["transform-es2015-arrow-functions", {"spec": true}],
["transform-class-properties", { "spec": false }],
"transform-object-rest-spread",
["transform-runtime", {
"helpers": true,
"polyfill": false,
"regenerator": false
}]
]
},
may be something wrong