1
\$\begingroup\$

I had a problem to define propTypes for my React class. I ran into solution that doesn't feel right:

let React = require('react')
let CallerCard = require('caller-card')
class CallerDetailsPanel {
 render() {
 return (
 <div className='caller-details-panel'>
 <CallerCard person={this.props.callerData.owner} />
 <CallerCard person={this.props.callerData.user} />
 </div>
 )
 }
}
CallerDetailsPanel.prototype.propTypes = {
 callerData: React.PropTypes.object
}
module.exports = React.createClass(CallerDetailsPanel.prototype)

Is this correct approach or how propTypes should be defined? If I try to define them inside class, I get a Parse error from 6to5 / esprima on console.

asked Jan 22, 2015 at 7:31
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I think you're just supposed to do this:

CallerDetailsPanel.propTypes = { callerData: React.PropTypes.object }

This feels a little backwards, but the React team has done this with the hope that in the future, with ES7, you will be able to use a syntax that looks something like this:

class CallerDetailsPanel {
 static propTypes = { callerData: React.PropTypes.object }
 render() {
 return (
 <div className='caller-details-panel'>
 <CallerCard person={this.props.callerData.owner} />
 <CallerCard person={this.props.callerData.user} />
 </div>
 )
 }
}
answered Apr 7, 2015 at 14:08
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.