Reputation: 21
I’m having an issue while accessing this.refs in the handleSubmit function.
My container looks like this:
import React, { Component } from 'react'
import { Link } from 'react-router'
import { connect } from 'react-redux'
import { getSomeStuff } from '../actions/someApiActions’
class MyClass extends Component {
componentWillMount() {
this.props.dispatch(getSomeStuffFromApi(this.props.params.cuid))
}
handleSubmit = () => {
console.log(this.refs) // always console logs an empty object {}
}
render() {
if(!this.props.results.item){
return (
<div>... loading …</div>
)
}
const { name } = this.props.results.item.stuff
return (
<div>
<p><input ref="name" defaultValue={ name }/></p>
<button type="submit" onClick={this.handleSubmit}>Update</button>
</div>
)
}
}
const mapStateToProps = (state) => {
return {
results: state.getSomeStuff
}
}
export default connect(mapStateToProps)(MyClass)
When I click on a submit button, handleSubmit will always logs an empty object (even though input is correctly rendered in html). I have noticed I can access this.refs in the componentDidUpdate method without any problem.
Why is accessing this.refs in handleSubmit not working?
Upvotes: 1
Views: 2147
Reputation: 21
LOL! The problem was in the babel translator. In the .babelrc I've been using this presets:
{
"presets": ["react", "node7", "stage-1"]
}
I had to change it to this:
{
"presets": ["react", "es2015", "stage-1"]
}
Upvotes: 1