简体   繁体   English

如何从渲染功能中分离JSX部件

[英]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.

相关问题 由另一个 function 组件渲染一个 JSX 部件 - reactJS - render a part JSX by another function component - reactJS 如何将渲染函数中的Jsx分离到ES6中的单独文件中? 或任何其他解决方案,以分离逻辑和演示? - How to Separate Jsx inside render function into a separate file in ES6? or any other solution in react to separate the Logic and presentation? 如何从函数中呈现JSX组件,以使其在屏幕上显示 - How to render a JSX component from the function show that it dispaly on screen 如何从父组件中的子组件呈现功能的jsx - how to render jsx of function from child component in parent component 如何在反应中从渲染函数返回一个空的jsx元素? - How to return an empty jsx element from the render function in react? 如何在React / Jsx中的渲染器内部调用函数 - How to Call a Function inside a Render in React/Jsx 如何将生成的HTML传递给JSX中的render函数? - How to pass generated HTML to the render function in JSX? Vue,如何在 JSX 渲染的道具中传递函数? - Vue, how to pass function in props in JSX render? 在 JSX 中自定义渲染 function - 如何处理片段 - Custom Render function in JSX - How to handle fragments 如何从类渲染方法中提取JSX? - How to extract JSX from class render method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM