Я очень новичок в React.js . Я установил Laravel 5.7 и заменил леса Vue.js на React, выполнив эту команду:
php artisan preset react
Теперь проблема в том, что я не могу ничего присвоить состоянию внутри компонента.
Например, если я сделаю следующее внутри компонента:
state = { foo: false };
Я получил ошибку:
ERROR in ./resources/js/components/Root.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: D:\xampp\htdocs\smart-school-v0.1\resources\js\components\Root.js: Support for the experimental syntax 'classProperties' isn't currently enabled (8:11):
Я установил:
@babel/plugin-proposal-class-properties
и обновленный .babelrc (файл конфигурации Babel), например:
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
Я следил за этим , но не повезло.
Package.json
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "npm run development -- --watch",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"@babel/plugin-proposal-class-properties": "^7.2.3",
"@babel/preset-env": "^7.2.3",
"@babel/preset-react": "^7.0.0",
"axios": "^0.18",
"babel-preset-react": "^6.23.0",
"bootstrap": "^4.0.0",
"cross-env": "^5.1",
"jquery": "^3.2",
"laravel-mix": "^4.0.13",
"lodash": "^4.17.5",
"popper.js": "^1.12",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"resolve-url-loader": "^2.3.1",
"sass": "^1.15.3",
"sass-loader": "^7.1.0"
},
"dependencies": {
"react-router-dom": "^4.3.1"
}
}
.babelrc
{
"presets": [
"@babel/preset-env",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
Root.js Компонент
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
export default class Root extends Component {
state = { foo: false };
render() {
return (
<p>Loaded</p>
);
}
}
if (document.getElementById('app')) {
ReactDOM.render(<Root />, document.getElementById('app'));
}
Он не работает должным образом, и я получаю эту ошибку:
Syntax Error: SyntaxError: D:\xampp\htdocs\smart-school-v0.1\resources\js\components\Root.js: Support for the experimental syntax 'classProperties' isn't currently enabled (19:11):
17 |
18 |
> 19 | state = { foo: false };
| ^
20 |
21 | render() {
22 |
Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.
@ ./resources/js/app.js 14:0-28
@ multi ./resources/js/app.js ./resources/sass/app.scss
Asset Size Chunks
Chunk Names
/css/app.css 0 bytes /js/app [emitted] /js/app
/js/app.js 593 KiB /js/app [emitted] /js/app
ERROR in ./resources/js/components/Root.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: D:\xampp\htdocs\smart-school-v0.1\resources\js\components\Root.js: Support for the experimental syntax 'classProperties' isn't currently enabled (19:11):
Пожалуйста, помогите мне.
Заранее спасибо.