Praveen Praveen - 8 months ago 73
React JSX Question

React native: watchID: ?number = null what is this syntax?

I was reading react native doc and I came across this line of code

watchID: ?number = null;

I know this is kind of
syntax but I can't see
being used in
React Native

Can someone please explain about this?

Answer Source

This is Facebook flow syntax. It's a way of introducing typing into regular Javascript. The source files can be statically parsed by flow to check for type problems. Then, when you compile your code with Babel, it takes these Flow annotations out so that it doesn't cause errors. You should see a .flowconfig file in your project's root directory—that's a dead giveaway your project is using flow.

This specific syntax is saying that if watchId is passed, it will be a number type, but the ? indicates that this is also a maybe type, meaning it may not be passed, in which case the = null will happen (this is not flow-related), which is how you assign a default value in JS.