[英]React-Native: Accessing methods in file included using require()
在 React Native 中將方法拆分為單獨的類文件時,我該如何從另一個文件訪問這些類?
例如在included.js我有以下內容:
'use strict'
import React, { Component } from 'react';
import {
AsyncStorage,
} from 'react-native';
var myClass = React.createClass({
doStuff: function(){
return 'Hello';
},
render: function(){ //This is required???
},
})
module.exports = myClass;
在我的 index.ios.js 文件中,我有:
var myClass = require('./included');
我現在如何從 myClass 調用函數?
我是使用 React Native 的新手,並懷疑我目前的方法並不理想,但還沒有找到如何分離我的代碼的明確示例。 例如,來自在我的應用程序中構建視圖的類的類中的外部 API 調用方法。
您不能只是簡單地調用另一個組件中的方法。 React 有自己的設計模式,但它不允許這樣做。
根據 React 文檔:
對於沒有父子關系的兩個組件之間的通信,您可以設置自己的全局事件系統。 在 componentDidMount() 中訂閱事件,在 componentWillUnmount() 中取消訂閱,並在收到事件時調用 setState()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.