最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - react-native : How to define a javascript class - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Mar 4, 2016 at 19:47 JohnJohn 751 gold badge1 silver badge5 bronze badges 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/… – Dany Khalife 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. – ArthurG Commented Oct 10, 2017 at 1:03
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论