ThunderHorse
ThunderHorse

Reputation: 375

React-Native: Accessing methods in file included using require()

When splitting methods into seperate class files in react native how do I then access those classes from another file?

For example in included.js I have the following:

'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;

In my index.ios.js file I have:

var myClass = require('./included');

How do I now call a function from myClass?

I'm new to using react native and suspect my current approach is not ideal but haven't found a clear example of how to seperate my code. For example external API calling methods in a class from the classes that build the views in my app.

Upvotes: 1

Views: 301

Answers (1)

abeikverdi
abeikverdi

Reputation: 1226

You can't just simply call a method in another component. React has its own design pattern and it does not allow that.
Accodring to React docs:

For communication between two components that don't have a parent-child relationship, you can set up your own global event system. Subscribe to events in componentDidMount(), unsubscribe in componentWillUnmount(), and call setState() when you receive an event.

Upvotes: 1

Related Questions