6

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.

2
  • i'm not familiar with reactjs but if you're looking to create a class in JS this should help, particularly the Custom objects section : developer.mozilla.org/en-US/docs/Web/JavaScript/… Commented Mar 4, 2016 at 19:59
  • ReactJS is a JavaScript library. React Native is a mobile framework, don't mistake them for the same thing. Commented Oct 10, 2017 at 1:03

1 Answer 1

20

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.