2017-06-05 106 views
0

爲什麼第一個渲染工作,但不是第二個?反應呈現不與getElementById

// load react library 
var React = require('react'); 
var ReactDOM = require('react-dom'); 

//build first component 
/* 
    StorePicker 
*/ 
var StorePicker = React.createClass({ 

    render : function(){ 
     return (
      <div> 
       <p>&nbsp;</p> 
       <p>hi</p> 
       <p>and this is another</p> 
      </div> 
     ) 
    } 
}) 


ReactDOM.render(<StorePicker/>,document.querySelector('#main')); 

ReactDOM.render(<StorePicker/>,document.getElementById('#main')); 

我得到的錯誤是:

Uncaught Error: Invariant Violation: _registerComponent(...): Target container is not a DOM element. 

不作任何意義,因爲它工作在第一種情況下

回答

3

getElementById一個id沒有#前綴參數。試試:

ReactDOM.render(<StorePicker/>, document.getElementById('main'));