reactjs/laravel 'npm run development' fails with syntax error 'Unexpected Token' - reactjs

I updated babel to the latest version and still get the error pointing to the "=" operator
35 | export const AssignmentsContainer = connectedContainer(
36 | class extends Component {
> 37 | buildAssignments = () => {
| ^
debug.log doesn't really offer any obvious hints
The package.json at this point looks like this:
{
"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": {
"axios": "^0.18.1",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"bootstrap": "^4.3.1",
"cross-env": "^5.1",
"jquery": "^3.4.1",
"laravel-mix": "^2.0",
"lodash": "^4.17.15",
"popper.js": "^1.12",
"react": "^16.9.0",
"react-dom": "^16.2.0"
},
"dependencies": {
"core-js": "^3.2.1",
"react-bootstrap": "^1.0.0-beta.12",
"react-html-parser": "^2.0.2",
"react-redux": "^7.1.1",
"redux": "^4.0.4"
}
}
Also tried sifting through this, but keep coming up empty: https://github.com/JeffreyWay/laravel-mix/issues/1402

Class properties are still not supported. So, you need to use #babel/plugin-proposal-class-properties which will help to transform class properties.
You need to install the babel plugin:
npm install --save-dev #babel/plugin-proposal-class-properties
And, in your .babelrc file, you need to make an entry:
"plugins": ["#babel/plugin-proposal-class-properties"]
This will solve your issue.
In the link you shared, it was mentioned to use "babel-plugin-transform-class-properties", but that's for older version of Babel. Since Babel v7 the name has been changed to "#babel/plugin-proposal-class-properties"
For Babel versions previous to 7:
Install the babel-plugin-transform-class-properties plugin:
npm install --save-dev babel-plugin-transform-class-properties
Then, make an entry in .babelrc:
"plugins": ["transform-class-properties"]

Related

React js error TypeError: React.memo is not a function

I am new in react js just now I have a install data-table component using npm install react-data-table-component styled-components and apply its basic example but its give me error TypeError: React.memo is not a function I have try to google its but all solutions about the redux currently I am using this my package.json file
{
"private": true,
"scripts": {
"dev": "npm run development",
"start": "react-scripts start",
"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/preset-react": "^7.0.0",
"axios": "^0.19",
"bootstrap": "^4.1.0",
"cross-env": "^5.1",
"jquery": "^3.2",
"laravel-mix": "^4.0.7",
"lodash": "^4.17.13",
"popper.js": "^1.12",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"resolve-url-loader": "^2.3.1",
"sass": "^1.15.2",
"sass-loader": "^7.1.0"
},
"dependencies": {
"mui-datatables": "^2.12.4",
"react-data-table-component": "^3.9.0",
"react-notifications-component": "^2.2.3",
"react-router-dom": "^5.1.2",
"styled-components": "^4.4.1"
}
}
Thanks
You are using react 16.2 but React.memo was released in 16.6. The library you are using requires the newer version of React.

Heroku is deploying old react?

Im using laravel framework (as rest api) and reactjs (to render views). I did a lot of changes to my code (local) and then pushed it on github and deploy on heroku. I noticed that react app is rendering old code version when laravel api works fine (i can use routes which i added). Why react didnt update?
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": {},
"dependencies": {
"axios": "^0.18",
"babel-preset-react": "^6.23.0",
"cross-env": "^5.1",
"jquery": "^3.2",
"laravel-mix": "^2.0",
"lodash": "^4.17.4",
"popper.js": "^1.12",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"gh-pages": "^2.0.1",
"react-icons": "^3.2.2",
"react-redux": "^5.0.7",
"react-scripts": "1.1.5",
"redux": "^4.0.0",
"styled-components": "^4.0.2",
"react-router-dom": "^4.3.1",
"redux-thunk": "^2.3.0"
}
}
May be the problem with your browser cache. Not in heroku. Please clear your browser cache and open your app again
Okay i figured it out! I need to render project on localhost to regenerate app.js in public/js before deploy on heroku -_-'

How can I config React Js and React Native in same project?

I want to use react js for web and react native for mobile in same project for redux and stylesheet.
I have seen such a git repos ReactNativeWebHelloWorld but I think the application is written in older versions so I could not get it to work.
I guess Expo is coming as default with new versions.
I could not get out of the problem.
Is it possible to do this with React 16 and React Native ^ 0.5, Expo ..?
edit : for package.json
{
"name": "ReactNativeWebHelloWorld",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "react-native-scripts start",
"ios-bundle": "react-native bundle --dev false --entry-file index.ios.js --platform ios --bundle-output ios/main.jsbundle",
"ios-dev-bundle": "react-native bundle --entry-file index.ios.js --platform ios --bundle-output ios/main.jsbundle",
"android-bundle": "react-native bundle --dev false --entry-file index.android.js --platform android --bundle-output android/app/src/main/assets/index.android.bundle",
"android-dev-bundle": "react-native bundle --entry-file index.android.js --platform android --bundle-output android/app/src/main/assets/index.android.bundle",
"web-bundle": "webpack --config web/webpack/web.prod.config.js --progress --colors",
"web-dev": "webpack-dev-server --content-base web/public/ --config web/webpack/web.dev.config.js --port 3001 --inline --hot --colors",
"clear-cache": "gulp clear-cache"
},
"dependencies": {
"babel": "^6.3.26",
"babel-polyfill": "^6.3.14",
"clone": "^2.1.1",
"morgan": "^1.6.1",
"object-assign": "^4.0.1",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-native": "^0.56.0",
"react-redux": "^5.0.7",
"redux": "^3.1.1",
"redux-thunk": "^2.3.0",
"superagent": "^3.8.3",
"expo": "^27.0.1"
},
"devDependencies": {
"autoprefixer": "^9.0.1",
"autoprefixer-loader": "^3.2.0",
"babel-core": "^6.4.5",
"babel-loader": "^7.1.5",
"babel-plugin-react-transform": "^3.0.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"css-loader": "^1.0.0",
"gulp": "^3.9.0",
"json-loader": "^0.5.4",
"less": "^3.8.0",
"less-loader": "^4.1.0",
"react-native-cli": "^2.0.1",
"react-transform-catch-errors": "^1.0.1",
"react-transform-hmr": "^1.0.1",
"redbox-react": "^1.3.2",
"redux": "^4.0.0",
"redux-devtools": "^3.3.1",
"redux-devtools-dock-monitor": "^1.1.1",
"redux-devtools-log-monitor": "^1.0.11",
"redux-logger": "^3.0.6",
"style-loader": "^0.21.0",
"webpack": "^4.16.3",
"webpack-dev-middleware": "^3.1.3",
"webpack-dev-server": "^3.1.5",
"webpack-hot-middleware": "^2.6.4",
"react-native-scripts": "1.14.0",
"jest-expo": "~27.0.0",
"react-test-renderer": "16.3.1"
},
"jest": {
"preset": "jest-expo"
}
}
Edit: I take this warnings and errors when I want to run on android emulator
E:\NewProjects\react-native-web-hello-world-master>react-native run-android
Scanning folders for symlinks in E:\NewProjects\react-native-web-hello-world-master\node_modules (65ms)Starting JS server...
Building and installing the app on the device (cd android && gradlew.bat installDebug)...
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details
cache fileHashes.bin (C:\Users\H³seyin\.gradle\caches\4.9\fileHashes\fileHashes.bin) is corrupt. Discarding.
> Configure project :app
WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation'.
It will be removed at the end of 2018
> Task :app:mergeDebugResources FAILED
Error: E:\NewProjects\react-native-web-hello-world-master\android\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml (Eri■im engellendi)
FAILURE: Build failed with an exception.
* What went wrong:
Failed to capture snapshot of output files for task ':app:mergeDebugResources' property 'incrementalFolder' during up-to-date check.
> Could not read path 'E:\NewProjects\react-native-web-hello-world-master\android\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log
output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BU¦LD FAILED in 17s
11 actionable tasks: 1 executed, 10 up-to-date
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/getting-started.html
I am running API as well react at same time
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"react": "webpack-dev-server --open --mode development",
"api": "nodemon ./bin/www",
"start": "concurrently \"npm run react\" \"npm run api\"",
"build": "webpack --mode production"
}

Lazy loading reactJS app with laravel backend

I am using laravel for backend and ReactJS for the frontend. I tried to use the asyncComponent method for loading components when needed, to decrease load time. The issue is that I am getting an error in dynamic import below:
const asyncCheckout = asyncComponent(() => {
return import('./containers/Checkout/Checkout');
});
I am getting error Unexpected token error on import keyword. I tried installing babel-plugin-syntax-dynamic-import but it still gives Unexpected token error. I am new to React and I would appreciate any help that can point me in the right direction. My .babelrc file has latest and react and env as presets. Thanks you.
My .babelrc file that is in project root:
{
"presets": [
"env"
],
"plugins": ["transform-class-properties"]
}
Web pack config, from laravel mix:
/**
* As our first step, we'll pull in the user's webpack.mix.js
* file. Based on what the user requests in that file,
* a generic config object will be constructed for us.
*/
require('../src/index');
require(Mix.paths.mix());
/**
* Just in case the user needs to hook into this point
* in the build process, we'll make an announcement.
*/
Mix.dispatch('init', Mix);
/**
* Now that we know which build tasks are required by the
* user, we can dynamically create a configuration object
* for Webpack. And that's all there is to it. Simple!
*/
let WebpackConfig = require('../src/builder/WebpackConfig');
module.exports = new WebpackConfig().build();
And my package.json file:
{
"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": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"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": {
"axios": "^0.17",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-latest": "^6.24.1",
"babel-preset-react": "^6.23.0",
"bootstrap-sass": "^3.3.7",
"cross-env": "^5.1",
"jquery": "^3.2",
"laravel-mix": "^1.0",
"lodash": "^4.17.4",
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"dependencies": {
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"react-bootstrap": "^0.31.5",
"react-redux": "^5.0.6",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"react-social-login": "^3.4.2",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"watch": "^1.0.2"
}
}
the best way i found to do a dynamic import is :)
use require() instead of import();
const HomeLazy = React.lazy(() => {
return new Promise((resolve) => {
setTimeout(() => resolve(require("./pages/home")), 300);
});
});

React webpack bundle for production

I need some help to bundle my output for production but I have no idea how. Here's my webpack.config.js
module.exports = {
devtool: 'eval',
context: __dirname,
entry: "./js-source/index.jsx",
output: {
path: __dirname + "/scripts",
filename: "bundle.js"
},
module: {
loaders: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
exclude: /node_modules/
}
],
resolve: {
extensions: ['', '.js', '.jsx', '.json']
}
}
};
Here's my package.json
{
"name": "citygross-store-web",
"version": "2.0.0",
"dependencies": {
"axios": "^0.14.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-redux": "^4.1.2",
"react-responsive": "^1.3.0",
"react-router": "^4.1.1",
"react-router-dom": "^4.1.1",
"react-transition-group": "^1.2.0",
"redux": "^3.1.2",
"redux-logger": "^2.6.1",
"redux-promise-middleware": "^4.0.0",
"redux-thunk": "^2.1.0"
},
"devDependencies": {
"babel-core": "^6.21.0",
"babel-loader": "^6.2.10",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-polyfill": "^6.3.14",
"babel-preset-es2015": "^6.24.1",
"babel-preset-latest": "^6.16.0",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-stage-1": "^6.24.1",
"babel-preset-stage-2": "^6.13.0",
"webpack": "^1.14.0",
"webpack-hot-middleware": "^2.12.2"
},
"scripts": {
"build": "webpack --colors",
"dev": "webpack --watch --colors"
},
"-vs-binding": {
"ProjectOpened": [
"dev"
]
}
}
How do you usually bundle for production? By adding a command like "npm run build" and having the other settings in that setting?
I would appreciate if someone could help me out how to set this up. Right now I can do "npm run dev" and "npm run build", however there's no difference between using the two more than "run dev" creates a -watcher and build builds the bundle one time.
Set the environment variable NODE_ENV to production when running your build script. This will cause the production build of React to be included in the output.
Easiest way is to add another script to your package.json:
"scripts": {
"build": "webpack --colors",
"buildprod": "NODE_ENV=production webpack --colors",
"dev": "webpack --watch --colors"
},
Now when you do npm run buildprod, you'll get a production build of your code.
Note: if you are on Windows, the above won't work right. For proper cross-OS you should use cross-env:
Install cross-env:
npm install cross-env --save-dev
or
yarn add cross-env --dev
Then make your scripts section look like:
"scripts": {
"build": "webpack --colors",
"buildprod": "cross-env NODE_ENV=production webpack --colors",
"dev": "webpack --watch --colors"
},

Resources