Felix Felix - 4 years ago 537
Javascript Question

Javascript :? operator

I can't figure out what this does or even is. Would someone please be so kind to point me what to research?

circle: (null : ?{ setNativeProps(props: Object): void }),

It's part of a React Native example and is part of a React Native class definition. The outer parts are (abbreviated):

var NavigatorIOSExample = React.createClass({
circle: (null : ?{ setNativeProps(props: Object): void }),

I assume it's some tricky use of the ternary operator. An anonymous function. But?

Source: https://facebook.github.io/react-native/docs/panresponder.html

Answer Source

The declaration is syntax from Flow. It says that 'circle' is an object with a property that is a function named 'setNativeProps':

{ setNativeProps(props: Object): void }

It also says that circle is nullable (indicated by the preceding '?') and that the default value will be null until an object of the specified type has been assigned to it.

If you look further down the sample you can see how calling code checks that circle has been assigned before calling setNativeProps:

this.circle && this.circle.setNativeProps({
    backgroundColor: CIRCLE_HIGHLIGHT_COLOR
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download