ReactのJSXでスプレッド属性を使おうとするとsyntax errorが出る対処法
Rails5.1のwebpakcerを使って、Reactの勉強をしているのですが、スプレッド属性を使ったときのコンパイルでSyntax errorが出ました。
21:42:08 hot.1 | ERROR in ./app/javascript/packs/hello_react.jsx
21:42:08 hot.1 | Module build failed: SyntaxError: Unexpected token (98:16)
21:42:08 hot.1 |
21:42:08 hot.1 | 96 |
21:42:08 hot.1 | 97 | ReactDOM.render(
21:42:08 hot.1 | > 98 | <h2 style = {{...myStyle}}>
21:42:08 hot.1 | | ^
21:42:08 hot.1 | 99 | Let's Spread!
21:42:08 hot.1 | 100 | </h2>,
21:42:08 hot.1 | 101 | document.getElementById('container')
何度も確認して記述ミスはなかったのですが、どうやらbabelのpluginが必要みたいでした。
babel-plugin-transform-object-rest-spreadのインストール
$ yarn add babel-plugin-transform-object-rest-spread
"plugins": [
"transform-object-rest-spread"
]
これだけで治りました。
Author And Source
この問題について(ReactのJSXでスプレッド属性を使おうとするとsyntax errorが出る対処法), 我々は、より多くの情報をここで見つけました https://qiita.com/uryonet/items/fa633db26d111b524fb5著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .