Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native : How to define a javascript class

I am using react native. I need to define a class:

class myClass {
  email: string;
  name: string;
  constructor() {
      setUser(fbid: string, token: string): boolean {

I am trying to define it in its own file myClass.js and when I include it in my index.ios.js , I get this error:

Can't find variable: myClass

Can you please point me to any documentation on how to define non react classes and use them in react native ? Thank you for reading.

like image 925
John Avatar asked Nov 30 '22 16:11

John


1 Answers

You need to export classes you define.

example:

//myClass.js
export default class myClass {
  email: string;
  name: string;
  constructor() {
      //...
  }
}

//index.ios.js
import myClass from './path/to/myClass.js'

Note the "export default", so you can define any class including non-react classes in a React Native (or Javascript es6) project and export it, making it available for import and use by other classes.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

for more details.

like image 166
tt9 Avatar answered Dec 03 '22 23:12

tt9