简体   繁体   English

React Hello World不适用于ES6

[英]React hello world doesn't work with ES6

What's wrong with my code? 我的代码有什么问题? I see no error in the console of jsbin. 我在jsbin的控制台中看不到任何错误。

http://jsbin.com/susumidode/edit?js,console,output http://jsbin.com/susumidode/edit?js,console,output

Class secondComponenent extends React.Component {
  render(){
    return (
      <p>My id is: {this.props.id}</p>
    );
  } 
} 


React.render(
  <secondComponenent id="abc">,
  document.getElementById('react_example')
);

And also how to render multiple component in React.render()? 还有如何在React.render()中渲染多个组件?

There were a couple of minor syntax errors holding you back. 有一些较小的语法错误使您退缩。 Lower case for class, upper case for naming the component, and closing off the component to be rendered. 类的小写字母,命名组件的大写字母,以及关闭要渲染的组件。 This code below works in your JSBin. 下面的代码可在您的JSBin中使用。

class SecondComponent extends React.Component {
  render(){
    return (
      <p>My id is: {this.props.id}</p>
    );
  } 
} 


React.render(
  <SecondComponent id="abc" />,
  document.getElementById('react_example')
);

First, you have to use ReactDOM to render your component to the browser not React . 首先,必须使用ReactDOM将组件呈现给浏览器而不是React You code is: 您的代码是:

React.render(
    <secondComponenent id="abc" />,
    document.getElementById('react_example')
);

But in recent versions of React (above 0.14) it must be: 但是在最新版本的React (高于0.14)中,它必须是:

ReactDOM.render(
  <secondComponenent id="abc" />,
  document.getElementById('react_example')
);

To make it work you can add this library to your html. 要使其工作,您可以将此添加到html中。

Second, you must close your component when it hasn't child components like this: <secondComponent id="abc" /> , your writing like this: <secondComponent id="abc"> . 其次,必须在没有子组件这样的子组件时关闭它: <secondComponent id="abc" /> ,这样子的写作是: <secondComponent id="abc">

In order to render multiple components in react, you have to wrap them with a single parent component for example like this: 为了在反应中呈现多个组件,您必须使用单个父组件包装它们,例如:

ReactDOM.render(
    <div>
        <firstComponenent id="abc" />
        <secondComponenent id="abc" />
    </div>,
  document.getElementById('react_example')
);

PS : Also, as @alexi2 says: class SomeComponent not Class SomeComponent . PS :另外,正如@ alexi2所说: class SomeComponent不是Class SomeComponent

If you wish to use props, you (may) need a constructor (please see the comments below this answer). 如果您想使用道具,则(可能)需要一个构造函数(请参阅此答案下方的注释)。

import React, { Component } from 'react';
import { render } from 'react-dom';

class SecoundComponent extends Component {
  constructor(props) {
    super(props);

    this.props = props;
  }

  render() {
    return (
      <p>My id is: {this.props.id}</p>
    )
  }
}

render(<SecoundComponent id="123" />, document.getElementById('react_example'));

Also note that I named the class SecoundComponent , with capital S. 另请注意,我将类SecoundComponent命名为大写S。

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

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