Vikram Jakkampudi Vikram Jakkampudi - 23 days ago 5
iOS Question

How can i export and import from one file to another in Ract Native

var SummarizeAll=React.createClass({
render:function(){

var self=this;
var org = this.props.org;
var root=this.props.root;
var name=rootSchema;
var current=root;

return (<ScrollView><View style={{marginLeft:5,marginRight:5}}>
<Text>{current.displayName}</Text>
{
this.state.records.map(function(data,index){

return (<GenreicApp/>)

},this)
}
</View>
</View>
</View>
</View>
</View>
</ScrollView>)
}
})


this is in index.ios.js

var GenericSummary=React.createClass({

render:function(){
return (<View>
<GenericDisplayView/>
<SummarizeAll />
</View>)
}
}
})


this is in index2.ios.js

how can i export SummarizeAll component into GenericSumnmary Component in index2.ios.js file from index.ios.js file

Answer

You need to use Javascript (or rather Node.js) modules:

  1. Place your SummarizeAll class in separate js file (summarize_all.js)
  2. At the end of the js file add module.exports=SummarizeAll;
  3. In the file that you want to use it use require directive: var SummarizeAll = require('./summarize_all');
  4. Then you can similarly require the SummarizeAll component in index.ios.js or elsewhere. Having it in separate file/subdirectory makes it standalone and reusable.

For clarity and better modularisation you can also place the .js file in some subdirectory, in such case require should contain the relative path to your file including subdirectory.