[英]How to separate the JSX part from render function
All: 所有:
I am pretty new to React.js and just use a little bit AngularJS, there is one question about React.js: 我对React.js很陌生,只使用了一点AngularJS,关于React.js有一个问题:
In AngularJS, the HTML part usually is separated from JS code as template, I like that way which make the code clean, I wonder if there is a similar way I can do this in React, or just use a function to apply this.state/this.props to the HTML part? 在AngularJS中,HTML部分通常与JS代码作为模板分开,我喜欢这种使代码整洁的方式,我想知道是否有类似的方法可以在React中做到这一点,或者只是使用函数来应用this.state /this.props到HTML部分?
Thanks 谢谢
The render
function is just a synchronous function. render
功能只是一个同步功能。 As long as the template has already been loaded by something you can absolutely do this. 只要模板已经被某种东西加载,您就可以做到这一点。
//Some module that has loaded the HTML
import jsxStore from 'jsxStore';
class SillyComponent extends React.Component {
render() {
return jsxStore.getSillyComponent(this.props, this.state);
}
}
The challenge here will be ensuring that the HTML is loaded by the time this call is made, since render
doesn't accept asynchronous results. 这里的挑战将是确保在进行此调用之前已加载HTML,因为
render
不接受异步结果。 The HTML will have to be loaded before the component tries to do anything. 在组件尝试执行任何操作之前 ,必须先加载HTML。
It should be noted that you are fighting React by doing this. 应该注意的是,您正在通过这样做来对抗React。 The React idea of a component is a self-contained bundle of code and presentation.
组件的React想法是一整套完整的代码和表示形式。 The goal is to keep the component in a single file, so that you can see the whole thing at once.
目的是将组件保存在单个文件中,以便您可以一次查看整个内容。 This is different from frameworks like Angular intentionally .
这是来自像角故意框架不同。 If you don't like this, React is not going to be a good fit for you.
如果您不喜欢它,React将不适合您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.