Hudson França
Hudson França

Reputation: 13

React native does not recognize static variable

I'm trying to use navigationOptions but reactNative doesn't recognize "static".

I've tried with classes and it worked, but with the function it didn't work

export default function Home() {
  static navigationOptions = {
    title: 'Home',
  };
  return (
    <View style={styles.container}>
      <Text style={styles.h1}>Home</Text>
    </View>
  );
}

Uncaught (in promise) TypeError: Cannot read property 'concat' of undefined
    at DeltaPatcher.applyDelta (DeltaPatcher.js:77)
    at deltaUrlToBlobUrl (deltaUrlToBlobUrl.js:28)
    at async getBlobUrl ((index):237)
    at async WebSocket.ws.onmessage ((index):192)

Upvotes: 1

Views: 254

Answers (1)

tvanlaerhoven
tvanlaerhoven

Reputation: 699

In your example, you can create a static variable shared by all instances like this:

export default function Home() {
  return (
    <View style={styles.container}>
      <Text style={styles.h1}>Home</Text>
    </View>
  );
}

Home.navigationOptions = {
  title: 'Home',
};

In JavaScript functions are first-class objects. So being an object, you can assign properties to a function.

In ES6 the class keyword was introduced with an accompanied static keyword.

Upvotes: 2

Related Questions