Unable to get arrow function to work - material-ui

I am trying to upgrade to es6 for an existing project with Babel. My package.json contains the babel plugins:
"devDependencies": {
"babel-cli": "^6.8.0",
"babel-core": "^6.0.0",
"babel-eslint": "^6.0.4",
"babel-loader": "^6.2.4",
"babel-plugin-transform-runtime": "^6.8.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-1": "^6.5.0",
"babel-preset-stage-2": "^6.5.0",
"babel-register": "^6.8.0",
"copy-webpack-plugin": "^2.1.3",
"css-loader": "^0.23.1",
"eslint": "^2.9.0",
"material-ui": "^0.15.0",
"node-sass": "^3.5.3",
"react-hot-loader": "^1.3.0",
"sass-loader": "^3.2.0",
"style-loader": "^0.13.1",
"webpack": "^1.13.0",
"webpack-dev-server": "^1.12.1"
My .babelrc file the following:
{
"presets": ["es2015", "stage-1", "react"],
"plugins": [
"transform-es2015-arrow-functions",
"transform-class-properties"
]
}
And my webpack file contains the following:
```
module: {
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
loaders: ['react-hot', 'babel-loader?presets[]=react,presets[]=es2015,presets[]=stage-2']
},
{
test: /\.scss$/,
loaders: ["style", "css", "sass"]
}
]}
```
And I run my node app with the following command:
cp views/index.html public/index.html && NODE_ENV=development webpack && npm run webpack-dev-server"
I am using a toggle function with material-ui in order to get Drawer component triggered by a hamburger menu in the Appbar component.
The Toggle functions are as follows (as per documentation)
```
handleToggle = () => this.setState({open: !this.state.open});
handleClose = () => this.setState({open: false});
``
However, whenever I run this I get the following error:
Unexpected token (29:16)
27 |
28 |
> 29 | handleToggle () => this.setState({open: !this.state.open});
| ^
30 |
If I change the function from arrow to a more old-school approach I can get it to work i.e.
handleToggle() {
this.setState({open: !this.state.open});
alert("handleToggle clicked");
}
However, I do not want to have to customise every component in material-ui in order to enable it to be backwards compatible. How do I upgrade my code without getting errors?

Related

Persistent Drawer not working with material-ui v5

I'm trying the Persistent Drawer example as it is in the documentation https://next.material-ui.com/components/drawers/#persistent-drawer, but it's not compiling.
/Users/alex/Dev/wnav-react2/src/App.tsx
TypeScript error in /Users/alex/Dev/wnav-react2/src/App.tsx(24,15):
Property 'open' does not exist on type '{ theme?: Theme | undefined; as?: ElementType<any> | undefined; sx?: SxProps<Theme> | undefined; } & ClassAttributes<HTMLElement> & HTMLAttributes<...> & { ...; }'. TS2339
22 |
23 | const Main = styled('main', { shouldForwardProp: (prop) => prop !== 'open' })(
> 24 | ({ theme, open }) => ({
| ^
25 | flexGrow: 1,
26 | padding: theme.spacing(3),
27 | transition: theme.transitions.create('margin', {
Any clue what could be the error?
package.json:
"#emotion/react": "^11.4.1",
"#emotion/styled": "^11.3.0",
"#material-ui/core": "^5.0.0-beta.4",
"#material-ui/icons": "^5.0.0-beta.4",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"#types/jest": "^26.0.15",
"#types/node": "^12.0.0",
"#types/react": "^17.0.0",
"#types/react-dom": "^17.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"typescript": "^4.1.2",
"web-vitals": "^1.0.1"
try
yarn add #mui/material #mui/icons-material #mui/styles

Server Discovery And Monitoring engine is deprecated, previous post doesn't help out

enter code here(node:20092) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(Use node --trace-warnings ... to show where the warning was created).
No previous solution works for me . Which i shown .
This are my dependencies.
"dependencies": {
"bcrypt": "^3.0.8",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"compression": "^1.7.4",
"config": "^3.3.6",
"cors": "^2.8.5",
"ejs": "^2.7.4",
"express": "^4.17.1",
"express-async-errors": "^3.1.1",
"fawn": "^2.1.5",
"fs": "0.0.1-security",
"helmet": "^3.23.3",
"joi": "^14.3.1",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"mongoose": "^5.13.2",
"nodemon": "^1.19.4",
"npm": "^6.14.13",
"path": "^0.12.7",
"popups": "^1.1.3",
"pug": "^2.0.4",
"supertest": "^4.0.2",
"winston": "^3.3.3",
"winston-mongodb": "^5.0.7"
},

How to fix Error cannot find module 'core-js/es6'

I have been getting an error regardless of what I do. In fact I am building an app with mongoDB as a server using mongoose and babel-watch, but every time I run babel-watch I get this error:
Error cannot find module 'core-js/es6'
I have installed and reinstalled core.js using npm install core.js, however nothing changes. Please help.
These are the dependencies I have installed on my project:
"dependencies": {
"body-parser": "^1.19.0",
"core-js": "^2.6",
"cuid": "^2.1.6",
"express": "^4.17.1",
"limax": "^1.7.0",
"mongoose": "^5.6.9",
"node-gyp": "^5.0.3"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^8.0.6",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-watch": "^7.0.0",
"nodemon": "^1.19.1"
}
}
Code error seen from terminal after running npm start
For core.js v3.9 import in App.tsx the next line: import "core-js/es";
Given that you are using core-js v2.6.9, your path should be core-js/library/es6 as the example shows you in the documentation here.

.babelrc `ignore` option is not ignoring the files that I want to be ignored

So the title pretty much explains my problem. I don't want babel to be transpiling some files that are nested within my folder structure, but I can't get it to work.
my babelrc file:
{
"presets" : ["es2015", "react", "stage-0", "es2017"],
"plugins": [
"transform-regenerator",
"transform-async-to-generator",
"transform-object-rest-spread",
],
"env": {
"development": {
"presets": ["react-hmre"]
}
},
"ignore": [
"node_modules",
"dist",
"public",
"src/client",
]
}
And my babel packages:
{
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-eslint": "^7.2.3",
"babel-loader": "^6.4.1",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-plugin-transform-regenerator": "^6.22.0",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.6.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2017": "^6.24.1",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-0": "^6.16.0","babel-register": "^6.18.0",
}
Any help would be greatly, greatly appreciated.
I believe you can do this through the CLI, but I'd much rather do it through the .babelrc file.
Thanks!
You just need to add --no-copy-ignored param to the end of your build script
babel src --out-dir lib --copy-files --no-copy-ignored
If you're using .babelrc or .babelrc.json, add ignore section to the root;
"ignore": [
"**/*.test.js"
]
Note: Tested on Babel 7.x
According to https://github.com/babel/babel/issues/5532#issuecomment-288755557:
There are some issues with ignore/only that we are fixing in 7.0
You could see if Babel 7 works well enough for you! According to their blog, it seems like it's working fine.
Otherwise, you might just be stuck doing it through the CLI for now.
https://github.com/babel/babel/pull/5487 seems to be the PR that fixed this issue for Babel 7.x.

Babel preset env not targeting correct env

I may be confused as to what babel-preset-env actually does. My assumption is that it would transpile my code for my targeted browsers including any necessary polyfills. This doesn't appear to be the case in my code though...
My code:
import "babel-polyfill"
...
var k = Object.values({ x: 's' });
My babel settings:
"presets": [
[ "env", {
"useBuiltIns": true,
"targets": {
"browsers": ["last 2 versions", "IE 10"]
}
}]
]
The transpiled code still contains Object.values and IE 10 dies quietly (it does transpile - I can see the rest of the code changing - just seems to target wrong).
Package.json (some of these imports are not in use):
"core-js": "^2.5.1",
"opentype.js": "^0.7.2",
"svg.draggable.js": "^2.2.1",
"svg.js": "^2.6.3",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.1",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-es2015-destructuring": "^6.23.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.0",
"babel-preset-latest": "^6.24.1",
"babel-register": "^6.24.1",
"clean-webpack-plugin": "^0.1.16",
"compression-webpack-plugin": "^1.0.0",
"copy-webpack-plugin": "^4.0.1",
"imports-loader": "^0.7.1",
"mocha": "^3.5.0",
"webpack": "^3.4.1",
"webpack-dev-server": "^2.8.2",
"webpack-merge": "^4.1.0"
babel-preset-env in its default state only handles converting syntax, not polyfilling standard library functionality.
Sounds like you'll want useBuiltins: true in your config. You'll also need to follow the other instructions there by installing core-js and adding an import for babel-polyfill.
Alternatively you can just load babel-polyfill itself and not rely on babel-preset-env at all.