简体   繁体   English

React:使用 ES6Class 创建一个组件并在没有 JSX 的情况下渲染它

[英]React: Creating A Component with ES6Class and rendering it without JSX

i have just started to learn React and i am trying to render components which i create as from classes.我刚刚开始学习 React,我正在尝试渲染我从类中创建的组件。 I like this way very much since it is similar to Java the language that i started with.我非常喜欢这种方式,因为它类似于我开始使用的语言 Java。

So far i want to understand how i can create a class from scratch and just render it on the page.I have searched around the web a bit and found some examples like here but they did not fit my situation because in that case, the user is using redux which i am not using.到目前为止,我想了解如何从头开始创建 class 并将其呈现在页面上。我在 web 周围搜索了一下,发现了一些类似这里的示例,但它们不适合我的情况,因为在这种情况下,用户正在使用我没有使用的 redux。 I also found several examples of rendering but they are not explicitly using classes.Could someone please show me how i can succesfully render the code to react-container i have here and explain it to me?我还发现了几个渲染示例,但它们没有明确使用类。有人可以告诉我如何成功地将代码渲染到我这里的react-container并向我解释吗? Your help will be greatly appreciated !对你的帮助表示感谢 !

class IngredientsList extends React.Component {

            renderListItem(ingredient, i) {
                return React.createElement("li", { key: i }, ingredient)
            }

            render() {
                return React.createElement("ul", { className: "ingredients" }, this.props.items.map(this.renderListItem))
            }

        }

        const myIngredients = new IngredientList();

        const items = ["1 lb Salmon", "1 cup pine nuts", "2 cups butter lettuce", "1 yellow squash", "1/2 cup Olive oil", "3 gloves of garlic"];

        const myList = myIngredients.renderListItem(items, 1);
        myIngredients.render();
<div id="react-container"></div>

so as you can see i have created the class, tried to initialize it and then use the render method but nothing displays.I suppose i have to somehow use this method here document.getElementById('react-container') to point where i want to render but i am not sure how to integrate that in my build.如您所见,我创建了 class,尝试对其进行初始化,然后使用渲染方法,但没有任何显示。我想我必须以某种方式在此处使用此方法document.getElementById('react-container')指向我想要的位置渲染,但我不确定如何将它集成到我的构建中。

In React, you never should call the render method on a component.在 React 中,你永远不应该在组件上调用render方法。 React handles the rendering for you, which is a big part of why to use React in the first place. React 为您处理渲染,这是首先使用 React 的重要原因。

You need to use ReactDOM.render to mount your app into the DOM, and then React will manage the rendering of components and sub-components from there.您需要使用ReactDOM.render将您的应用程序挂载到 DOM 中,然后 React 将从那里管理组件和子组件的渲染。

ReactDOM.render accepts two arguments. ReactDOM.render接受两个 arguments。 The first is the component, and the second is the actual DOM element where you want your app to be rendered on the page.第一个是组件,第二个是您希望应用在页面上呈现的实际 DOM 元素。

 class IngredientsList extends React.Component { renderListItem(ingredient, i) { return React.createElement("li", { key: i }, ingredient) } render() { return React.createElement("ul", { className: "ingredients" }, this.props.items.map(this.renderListItem)) } } const items = ["1 lb Salmon", "1 cup pine nuts", "2 cups butter lettuce", "1 yellow squash", "1/2 cup Olive oil", "3 gloves of garlic"]; ReactDOM.render( React.createElement(IngredientsList, {items: items}), document.getElementById('react-container') );
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="react-container"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM