react-native:如何定义一个javascript类 [英] react-native : How to define a javascript class

查看:33
本文介绍了react-native:如何定义一个javascript类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用本机反应.我需要定义一个类:

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

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

我试图在它自己的文件 myClass.js 中定义它,当我将它包含在我的 index.ios.js 中时,我收到此错误:

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.

推荐答案

您需要导出您定义的类.

You need to export classes you define.

示例:

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

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

请注意导出默认值",因此您可以在 React Native(或 Javascript es6)项目中定义任何类,包括非反应类并将其导出,使其可供导入和使用其他班级.

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.

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

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

了解更多详情.

这篇关于react-native:如何定义一个javascript类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆