Giuseppe Turturiello
Giuseppe Turturiello

Reputation: 341

React PropTypes.oneOf to specify an enum doesn't work

I have a problem to specify a property of type 'enum' in react. According to the documentation here React multiple components the following snippet should work :

position : React.PropTypes.oneOf(['rightTop','rightBottom'])

But I get the following error

ERROR in ./app/components/mqttComponents/mqttPresence.jsx
Module build failed: SyntaxError:/Users/giuseppe/Projects/sw-director/app/components/mqttComponents/mqttPresence.jsx: Unexpected token (68:36)
66 |   propTypes : {
67 |     //position: React.PropTypes.string.isRequired,
> 68 |     position : React.PropTypes.oneOf(['rightTop','rightBottom']),
                                           ^
 69 |     showMqttClientStatus : React.PropTypes.bool.isRequired,
 70 |     mqtt: React.PropTypes.object
 71 |   }

I don't realise which is the error? Maybe is something related to the new ES6 syntax ?

Upvotes: 33

Views: 34571

Answers (1)

zbrox
zbrox

Reputation: 2723

With the ES6 syntax the propTypes in React should be defined as a static property. So the only difference should be in the propTypes declaration.

static propTypes = {
     position : React.PropTypes.oneOf(['rightTop','rightBottom']),
     showMqttClientStatus : React.PropTypes.bool.isRequired
}

Upvotes: 61

Related Questions