React.js: how to decouple jsx out of JavaScript

前端 未结 5 1238
你的背包
你的背包 2021-01-30 00:08

Is there any way to move the jsx from a component\'s render function to a separate file? If so, how do I reference the jsx in the render function?

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-30 00:25

    I just separated JSX into anonymous function files

    template.js

    export default (component) => {
    return 

    Hello {component.props.name}

    }

    my-component.js

    import React, {Component} from 'react';
    import template from './template';
    
    export default MyComponent extends Component {
       render() {
         return template(this);
       }
    }
    

    In template you can access props or state or functions using component variable.

提交回复
热议问题