2017-12-05 3 views
1

Reactjs를 배우는 데 매우 흥미가 있습니다. YouTube에서 자습서를 보았습니다.반응 애플 리케이션의 출력이 없습니다

나는 또한 tutorial과 같은 인터넷에서 다른 튜토리얼을 따랐다.

내 주요 문제는 위에서 언급 한 튜토리얼에서 첫 번째 예제를 실행하려고하면 코드가 결과를 얻지 못하고 뭔가 놓쳤습니까? (내가하게 IntelliJ IDEA를 사용합니다.)

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8" /> 
    <title>React tutorial</title> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script> 

    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
</head> 
<body> 

<div id="content"></div> 

<script type="text/jsx"> 

    var TimerExample = React.createClass({ 

     getInitialState: function(){ 

      // This is called before our render function. The object that is 
      // returned is assigned to this.state, so we can use it later. 

      return { elapsed: 0 }; 
     }, 

     componentDidMount: function(){ 

      // componentDidMount is called by react when the component 
      // has been rendered on the page. We can set the interval here: 

      this.timer = setInterval(this.tick, 50); 
     }, 

     componentWillUnmount: function(){ 

      // This method is called immediately before the component is removed 
      // from the page and destroyed. We can clear the interval here: 

      clearInterval(this.timer); 
     }, 

     tick: function(){ 

      // This function is called every 50 ms. It updates the 
      // elapsed counter. Calling setState causes the component to be re-rendered 

      this.setState({elapsed: new Date() - this.props.start}); 
     }, 

     render: function() { 

      var elapsed = Math.round(this.state.elapsed/100); 

      // This will give a number with one digit after the decimal dot (xx.x): 
      var seconds = (elapsed/10).toFixed(1); 

      // Although we return an entire <p> element, react will smartly update 
      // only the changed parts, which contain the seconds variable. 

      return <p>This example was started <b>{seconds} seconds</b> ago.</p>; 
     } 
    }); 


    ReactDOM.render(
      <TimerExample start={Date.now()} />, 
     document.getElementById('content') 
    ); 
</script> 

</body> 
</html> 
+0

당신은 콘솔에서 오류를받을 수 있나요? –

+0

콘솔에 오류 메시지가 표시됩니까? 어쨌든 당신의 튜토리얼은 매우 오래된 것 같습니다. – Axnyff

+0

오류 메시지가 나타나지 않고 웹 페이지가 열리지 만 비어 있습니다. – Pro

답변

1

오전 내가 뭔가를 놓친?

예, "React"와 "ReactDOM"의 두 가지 중요한 부분을 혼합했습니다. 처음에는 모든 코드가 포함 된 단일 lib가 반응했지만 v0.14 이후에는 단일 lib가 두 부분으로 나뉘어 "react"와 "react-dom"으로 나뉘어졌습니다. 자세한 내용은

는이를 확인 : React vs ReactDOM?

해결 문제에 : 당신은 V0.13을 사용하는

그래서 React.render 대신 ReactDOM.render 사용합니다. 당신이 참조를 제거 할 수 있도록 또한, JQuery 필요하지 않습니다

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 

: 또 다른 가능한 솔루션은 이러한 스크립트 (별도 libs와)를 사용합니다.

제안 자세한 업데이트 및 더 나은 방법에 대한 doc을 확인합니다.

React.render를 사용하여 니펫을 사용 :

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="UTF-8" /> 
 
    <title>React tutorial</title> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/react.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.3/JSXTransformer.js"></script> 
 
</head> 
 
<body> 
 

 
<div id="content"></div> 
 

 
<script type="text/jsx"> 
 

 
    var TimerExample = React.createClass({ 
 

 
     getInitialState: function(){ 
 

 
      // This is called before our render function. The object that is 
 
      // returned is assigned to this.state, so we can use it later. 
 

 
      return { elapsed: 0 }; 
 
     }, 
 

 
     componentDidMount: function(){ 
 

 
      // componentDidMount is called by react when the component 
 
      // has been rendered on the page. We can set the interval here: 
 

 
      this.timer = setInterval(this.tick, 50); 
 
     }, 
 

 
     componentWillUnmount: function(){ 
 

 
      // This method is called immediately before the component is removed 
 
      // from the page and destroyed. We can clear the interval here: 
 

 
      clearInterval(this.timer); 
 
     }, 
 

 
     tick: function(){ 
 

 
      // This function is called every 50 ms. It updates the 
 
      // elapsed counter. Calling setState causes the component to be re-rendered 
 

 
      this.setState({elapsed: new Date() - this.props.start}); 
 
     }, 
 

 
     render: function() { 
 

 
      var elapsed = Math.round(this.state.elapsed/100); 
 

 
      // This will give a number with one digit after the decimal dot (xx.x): 
 
      var seconds = (elapsed/10).toFixed(1); 
 

 
      // Although we return an entire <p> element, react will smartly update 
 
      // only the changed parts, which contain the seconds variable. 
 

 
      return <p>This example was started <b>{seconds} seconds</b> ago.</p>; 
 
     } 
 
    }); 
 

 

 
    React.render(
 
      <TimerExample start={Date.now()} />, 
 
     document.getElementById('content') 
 
    ); 
 
</script> 
 

 
</body> 
 
</html>

+0

감사합니다. 당신의 대답은 :) – Pro

관련 문제