2017-10-29 20 views
-1

ここに反応します。 code snippetでは、を避けるために、関数handleChangeをES6スタイルに変更すると、の後に=にエラーが発生します。なぜだろう。Reactjs私のコードスニペットのES6構文エラー

===あなたが割り当てた後にセミコロンが欠落している私の変更==

function BoilingVerdict(props) { 
    if (props.celsius >= 100) { 
    return <p>The water would boil.</p>; 
    } 
    return <p>The water would not boil.</p>; 
} 

class Calculator extends React.Component { 
    constructor(props) { 
    super(props); 
    //this.handleChange = this.handleChange.bind(this); 
    this.state = {temperature: ''}; 
    } 

    handleChange = (e) => { 
    this.setState({temperature: e.target.value}); 
    } 

    render() { 
    const temperature = this.state.temperature; 
    return (
     <fieldset> 
     <legend>Enter temperature in Celsius:</legend> 
     <input 
      value={temperature} 
      onChange={this.handleChange} /> 
     <BoilingVerdict 
      celsius={parseFloat(temperature)} /> 
     </fieldset> 
    ); 
    } 
} 

ReactDOM.render(
    <Calculator />, 
    document.getElementById('root') 
); 
+2

どのようなエラーが表示されますか?私は割り当て後にセミコロンがないと信じています。 – Sulthan

+0

@Sulthanさん、ありがとうございました。まさにエラーの原因です。私はいつも必要というわけではないので、JSでは ';'を持たないことに慣れてきました。あなたが回線の回答を書き留めたら、私はそれを受け入れます! – pktCoder

答えて

0

とコード:

handleChange = (e) => { 
    this.setState({temperature: e.target.value}); 
}; 

エラー( "予期しないトークン")はおそらくちょうどエディタですエラー。セミコロンを自動的に挿入する必要があるため、機能には影響しません。

enter image description here

+0

JavaScriptではセミコロンはオプションです。 – Cristy

+0

@Cristy Nope。 JSではセミコロンが自動的に挿入されます。それは同じではありません。 – Sulthan

+0

しかし、彼がそれを追加するかどうかの違いは何ですか?パーサーはそれをとにかく追加しますが、それはちょうど糸くずれのエラーですか? – Cristy

0

あなたはes2015にバベルでコードを解析する必要があります。あなたのステートメントはクラスプロパティなので、transform-class-propertiesプラグイン(それはes2015プリセットの一部です)