我正在研究React項目,我嘗試編譯但找不到爲什麼會出現此語法錯誤。具體來說,在這種情況下,模式「{()=> {}()}」在做什麼?JSX內部立即調用的函數表達式
Module build failed: SyntaxError: Unexpected token, expected } (35:9)
33 | return (<div className="loading" />);
34 | }
35 | }()}
| ^
36 | </div>
37 | );
38 | }
@ ./src/containers/SearchApp.js 7:0-52
@ ./src/containers/App.js
@ ./src/index.js
@ multi ./src/index
的代碼的一部分:
render() {
return (
<div>
<div className="form-group">
<input onKeyDown={this.searchEnter.bind(this)} type="text" ref="tag" className="form-control input-lg search-bar" placeholder="Enter the tag..." />
<button onClick={this.searchClick.bind(this)} type="button" className="btn btn-default search-button">Search!</button>
</div>
{()=>{
if (this.props.status === 'PENDING') {
return (<div className="loading" />);
}
}()}
</div>
);
看起來像有人拿了一個老同學IIFE,'函數(){...}()',並試圖使其成爲性感()=> {...})()' –
Jaromanda是正確的inline IIFE的模式是()=> {...}()不知道箭頭IIFE的必須是'(()=> {})()'。請記住,在IIFE的函數中,第一組括號中的內容將被評估。然後第二組圓括號將調用該函數。 – Rodrigo
謝謝你的評論...!在它周圍添加一個包裝解決了語法錯誤..!我剛開始學習JavaScript,需要學習這麼多。我總是感謝你的幫助。 – tak