【问题标题】:getElementById reference error using react使用反应的getElementById参考错误
【发布时间】:2016-10-01 01:30:15
【问题描述】:

https://jsbin.com/nisetupaqa/edit?html,js,console,output

 var App = React.createClass({
      render(){
        return(
        <h1>Hello world</h1>
        )
      }
    })

ReactDOM.render(<App />,getElementById('app-container'));

怎么了?无法让 hello world 正常工作,我收到了 "ReferenceError: getElementById is not defined 的错误

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

getElementByIddocument 对象的一个​​方法,它不是一个全局的。

document.getElementById 将由浏览器定义。

如果你想将getElementById 称为全局,那么你必须自己定义它。

【讨论】:

    【解决方案2】:

    这应该可行:

    class App extends React.Component {
      handleClick(e){
        alert("Clicked");
      }
      render(){
          return(
             <h1 onClick={this.handleClick.bind(this)}>Hello world</h1>
        )
      }
    }
    
    ReactDOM.render(<App />,document.getElementById('app-container'));
    

    【讨论】:

      猜你喜欢
      • 2014-11-26
      • 2021-10-03
      • 2017-11-11
      • 2013-05-25
      • 1970-01-01
      • 2019-10-27
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多