Importing MUI5 TextField results in "Object(...) is not a function" - material-ui

So, I am basically converting a project from:
Material-UI 0.19 to MUI 5.
babel-core 6.25 to #babel/core 7.16
React 16.3 to React 17.0.2
After installing all the required dependencies (see dependencies below), adjusting the source code to use the new MUI5 components, I am met with the error below:
TypeError: Object(...) is not a function
This is raised after importing the line
import TextField from '#mui/material/TextField';
Some portion of the stack trace:
.../node_modules/#emotion/cache/dist/emotion-cache.browser.esm.js:287
284 | } : rulesheet(function (rule) {
285 | currentSheet.insert(rule);
286 | })];
> 287 | var serializer = middleware(omnipresentPlugins.concat(stylisPlugins, finalizingPlugins));
288 |
289 | var stylis = function stylis(styles) {
290 | return serialize(compile(styles), serializer);
.../node_modules/#emotion/react/dist/emotion-element-99289b21.browser.esm.js:17
14 | // and we could have a special build just for that
15 | // but this is much easier and the native packages
16 | // might use a different theme context in the future anyway
> 17 | typeof HTMLElement !== 'undefined' ? /* #__PURE__ */createCache({
18 | key: 'css'
19 | }) : null);
20 |
Dependencies added
"#babel/core": "^7.16.0",
"#babel/preset-env": "^7.16.0",
"#babel/preset-react": "^7.16.0",
"#emotion/react": "^11.5.0",
"#emotion/styled": "^11.3.0",
"#material-ui/core": "^5.0.0-beta.5",
"#material-ui/styles": "^5.0.0-beta.5",
"#mui/icons-material": "^5.0.5",
"#mui/material": "^5.0.4",
"babel-core": "^7.0.0-bridge.0",
"babel-loader": "^7.1.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"terser-webpack-plugin-legacy": "^1.2.5",
Babel preset
"babel": {
presets": [
"#babel/preset-env",
"#babel/preset-react"
]
},
Webpack entry
{
test: /\.(js|jsx)$/,
include: paths.appSrc,
exclude: /node_modules/,
loader: require.resolve('babel-loader'),
options: {
compact: true,
},
},

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

react-native-sensors Error: Native modules for sensors not available. Did react-native link run successfully?

Thanks for any help, getting error message "Error: Native modules for sensors not available. Did react-native link run successfully?" when running npx react-native start
npx react-native run-android gives this error: > Task :app:processDebugManifest FAILED
right after installing react-native-sensors.
Install procedure:
npx react-native init proj8
cd proj8
npm install react-native-sensors --save
npx react-native link react-native-sensors
The last command reported successful but when I went through the manual Android procedure some parts were not done as follows:
import com.sensors.RNSensorsPackage;
was in android/app/src/main/java/[...]/MainApplication.java
new RNSensorsPackage()
was not there, I added as follows:
#Override
protected List<ReactPackage> getPackages() {
#SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(new RNSensorsPackage());
return packages;
}
include ':react-native-sensors'
project(':react-native-sensors').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sensors/android')
were both there in android/settings.gradle
dependencies {
implementation project(':react-native-sensors')
implementation fileTree(dir: "libs", include: ["*.jar"])
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+" // From node_modules
I had to add implementation project(':react-native-sensors')
I don't think my code matters yet but App.js:
import React, {useState, useEffect, useRef} from 'react';
import { StyleSheet,ScrollView,SafeAreaView,Platform, View,Text } from 'react-native';
export default function App() {
var Rndr =
<Text>asdf</Text>
return Rndr
}
package.json:
{
"name": "proj8",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"react": "16.13.1",
"react-native": "0.63.3",
"react-native-sensors": "^7.0.1"
},
"devDependencies": {
"#babel/core": "^7.8.4",
"#babel/runtime": "^7.8.4",
"#react-native-community/eslint-config": "^1.1.0",
"babel-jest": "^25.1.0",
"eslint": "^6.5.1",
"jest": "^25.1.0",
"metro-react-native-babel-preset": "^0.59.0",
"react-test-renderer": "16.13.1"
},
"jest": {
"preset": "react-native"
}
}
I encountered the same problem. This occurs because react-native-sensors has a minsdk version is 17 and react-native's default minsdk version is 16 Its quite easy to solve.
Just go to android/build.gradle
Change minSdkVersion to anything above 17
Before
ext {
buildToolsVersion = "29.0.2"
minSdkVersion = 16
compileSdkVersion = 29
targetSdkVersion = 29
}
After
ext {
buildToolsVersion = "29.0.2"
minSdkVersion = 19
compileSdkVersion = 29
targetSdkVersion = 29
}

How to create a multi-apps project in Ionic 4?

I try to create a simple multi-apps project in Ionic 4, following the ionic documentation, but it doesn't work.
https://ionicframework.com/docs/cli/configuration#multi-app-projects
My config :
Ionic : v4.12.0
Angular CLI : 7.3.8
Node : 10.15.1
npm : 6.9.0
Windows 10
What I have done :
I have created a directory “monorepo”.
In this folder, I created a file ionic.config.json and pasted in it the code below:
{
"projects": {}
}
In monorepo, I create a second directory : apps.
In this directory (with the command prompt), I write the command :
ionic start "app1" --no-deps
monorepo/ionic.config.json is correctly populated with the data:
{
"projects": {
"app1": {
"name": "app1",
"integrations": {},
"type": "angular",
"root": "apps\\app1"
}
}
}
But when I try to serve the server, it won't work.
ionic serve --project app1 return me this error message :
> ng run app1:serve --host=0.0.0.0 --port=8100
[ng] Project 'app1' could not be found in workspace.
[ng] Error: Project 'app1' could not be found in workspace.
[ng] at Workspace.getProject (C:\Users\name\AppData\Roaming\npm\node_modules\#angular\cli\node_modules\#angular-devkit\core\src\workspace\workspace.js:93:19)
[ng] at Architect.getBuilderConfiguration (C:\Users\name\AppData\Roaming\npm\node_modules\#angular\cli\node_modules\#angular-devkit\architect\src\architect-legacy.js:117:41)
[ng] at RunCommand.runSingleTarget (C:\Users\name\AppData\Roaming\npm\node_modules\#angular\cli\models\architect-command.js:160:45)
[ng] at RunCommand.runArchitectTarget (C:\Users\name\AppData\Roaming\npm\node_modules\#angular\cli\models\architect-command.js:201:35)
[ng] at RunCommand.run (C:\Users\name\AppData\Roaming\npm\node_modules\#angular\cli\commands\run-impl.js:14:25)
[ng] at RunCommand.validateAndRun (C:\Users\name\AppData\Roaming\npm\node_modules\#angular\cli\models\command.js:124:31)
[ng] at process._tickCallback (internal/process/next_tick.js:68:7)
[ng] at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
[ng] at startup (internal/bootstrap/node.js:283:19)
[ng] at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
[ERROR] ng has unexpectedly closed (exit code 1).
The Ionic CLI will exit. Please check any output above for error details.
When I go under the file associated with the error (workspace.js:93:19), and I do console.log, I obtain this info:
getProject(projectName) {
this._assertLoaded();
console.log("this.projectName : " + projectName); // this.projectName : app1
console.log("\n\nthis._workspace.projects : \n");
console.log(this._workspace.projects); // this._workspace.projects : app and app-e2e
const workspaceProject = this._workspace.projects[projectName];
(Values displayed are in comments).
The projectName variable seems ok. But the available project in this._workspace.projects are app and app-e2e which seem weird.
What is my problem ?
What did I miss ?
I managed to compile by replacing in the angular.json every app keyword by the name of my ionic project (here, app1).
{
"$schema": "./node_modules/#angular-devkit/core/src/workspace/workspace-schema.json",
"version": 1,
"defaultProject": "app1", <-- HERE
"newProjectRoot": "projects",
"projects": {
"app1": { <-- HERE
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app1", <-- NOT MANDATORY
"schematics": {},
"architect": {
"build": { ... },
"serve": {
"builder": "#angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "app1:build" <-- HERE
},
...
}
I suppose that instead of putting several angular apps within one project structure you should rewrite each ionic project's angular.json

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.

Unable to get arrow function to work

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?