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

javascript - Call render from different class in React Native - Stack Overflow

programmeradmin1浏览0评论

I want to seperate my rendering of a class in another class and just call the rendering. Do you know how to do this in React Native? Would be great if you can help me out here.

Thanks a lot

Albo

I want to seperate my rendering of a class in another class and just call the rendering. Do you know how to do this in React Native? Would be great if you can help me out here.

Thanks a lot

Albo

Share Improve this question asked Jul 7, 2016 at 5:05 NullPointerNullPointer 1,3292 gold badges12 silver badges20 bronze badges 1
  • I just wanted to seperate rendering and logic. For example you have a class Home which has business logic and rendering for your view. I want to seperate business logic in one class and rendering of your view in another class. Question now clear? Sorry for that... – NullPointer Commented Jul 7, 2016 at 5:20
Add a ment  | 

3 Answers 3

Reset to default 3

Got it!

You have to write a Class like this for example:

export default class HomeRender extends Component {
constructor(props){
    super(props);
}

render() {
    return (
      <View>
          <Text>
              Hi
          </Text>
      </View>
    );
} 
}
module.exports = HomeRender;

After that you have simple to do the following in your class to call the render function from HomeRender:

var Home = require('./app/ponents/home/HomeRender');
...
render() {
 <View>
  <Home />
 </View>

}
...

Calling render from another class is not good practice. Instead create a new ponent class and import that class in your view. It will render.

This is not a good practise man, in react-native you should always import your ponent and insert it wherever you want like this:

Import your render file which you want like this:

            var Home = require('./app/ponents/home/HomeRender');

Again, wherever yo want to import that ponent which consists certain view you can render inside your specific like this:

render() {
   <View>
       your wish ponent.... <Home/>
    </View>
  }

This will ease your work in navigation part as well as to render your ponents which return view in wherever you want. Also, you should follow official documentation of react-native as well: https://facebook.github.io/react-native/docs/tutorial.html

发布评论

评论列表(0)

  1. 暂无评论