2015-06-02 25 views
10

Tôi là một người mới phản ứngLỗi không bắt buộc: Vi phạm bất biến: React.render(): Thành phần thành phần không hợp lệ

và tôi đang tạo một lớp và chức năng đơn giản và hiển thị cho nội dung.

Tuy nhiên,

tôi nhận được một Uncaught Error: Invariant Violation: React.render(): Invalid component element.

<script src="https://fb.me/react-0.13.3.js"></script> 
<script src="https://fb.me/JSXTransformer-0.13.3.js"></script> 
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script> 
<script type="text/jsx"> 

    var HelloWorld = React.createClass({ 
     render: function() { 
      return <div>Hello, world!</div>; 
     } 
    }); 

    React.render(new HelloWorld(), document.body); 

</script> 


<body> 

</body> 
</html> 

Bất kỳ ý tưởng về những gì là sai?

+0

Bản sao có thể có của [Thành phần không hợp lệ trong React.JS] (http://stackoverflow.com/questions/30275793/invalid-component-element-in-react-js) – Mono

Trả lời

13

Thay đổi React.render(new HelloWorld(), document.body); để React.render(React.createElement(HelloWorld), document.body); và nó sẽ làm việc. Chức năng The React.render mong đợi một ReactElement mà bạn có thể tạo thông qua React.createElement.

Bạn có thể xem tài liệu ở đây cùng với một số chức năng hữu ích khác: https://facebook.github.io/react/docs/top-level-api.html

16

sử dụng <HelloWorld/> thay vì new HelloWorld()

+1

Điều này đã giúp tôi, tôi đã có 'ReactDOM .render ('', document.getElementById ('app')); 'nhưng cần có' ReactDOM.render (, document.getElementById ('app')); '- không có dấu ngoặc kép xung quanh' ' – house9

+0

I có nó mà không có dấu ngoặc kép và vẫn gặp lỗi: / – guillefix

0

Viết họ phương pháp thành phần của bạn làm như thế này sẽ khởi chạy Lỗi cùng:

... 
render: function() { 
    return  //you need "return <div>" for this to work. 
    <div> 
     <h4>MyComponent message</h4> 
    </div>; 
} 

Mã hiển thị sẽ kích hoạt cùng một lỗi bởi vì bây giờ có phần tử bên cạnh sự trở lại (bạn không nên phá vỡ dòng đó).

Các vấn đề liên quan