6

I use exactly the code as defined in the example from https://material-ui.com/components/bottom-navigation/:

// saved in app.tsx:

import React from 'react';

import { makeStyles } from '@material-ui/core/styles';
import BottomNavigation from '@material-ui/core/BottomNavigation';
import BottomNavigationAction from '@material-ui/core/BottomNavigationAction';
import FolderIcon from '@material-ui/icons/Folder';
import RestoreIcon from '@material-ui/icons/Restore';
import FavoriteIcon from '@material-ui/icons/Favorite';
import LocationOnIcon from '@material-ui/icons/LocationOn';

const useStyles = makeStyles({
  root: {
    width: 500,
  },
});


export default function LabelBottomNavigation() {
  const classes = useStyles();
  const [value, setValue] = React.useState('recents');

  const handleChange = (event: React.ChangeEvent<{}>, newValue: string) => {
    setValue(newValue);
  };

  return (
    <BottomNavigation value={value} onChange={handleChange} className={classes.root}>
      <BottomNavigationAction label="Recents" value="recents" icon={<RestoreIcon />} />
      <BottomNavigationAction label="Favorites" value="favorites" icon={<FavoriteIcon />} />
      <BottomNavigationAction label="Nearby" value="nearby" icon={<LocationOnIcon />} />
      <BottomNavigationAction label="Folder" value="folder" icon={<FolderIcon />} />
    </BottomNavigation>
  );
}

However when using this component in index.tsx:

// saved in index.tsx

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(
  <React.StrictMode>
    <App/>
  </React.StrictMode>,
  document.getElementById('root'),
);

I get this error:

Unhandled Runtime Error

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

Check the render method of `ForwardRef(BottomNavigationAction)`.

My package.json:

{
  "scripts": {
    "start": "snowpack dev",
    "build": "snowpack build",
    "test": "web-test-runner \"src/**/*.test.tsx\"",
    "format": "prettier --write \"src/**/*.{js,jsx,ts,tsx}\"",
    "lint": "prettier --check \"src/**/*.{js,jsx,ts,tsx}\""
  },
  "dependencies": {
    "@material-ui/core": "^4.11.3",
    "@material-ui/icons": "^4.11.2",
    "react": "^17.0.0",
    "react-dom": "^17.0.0"
  },
  "devDependencies": {
    "@snowpack/plugin-dotenv": "^2.0.5",
    "@snowpack/plugin-react-refresh": "^2.4.0",
    "@snowpack/plugin-typescript": "^1.2.0",
    "@snowpack/web-test-runner-plugin": "^0.2.0",
    "@testing-library/react": "^11.0.0",
    "@types/chai": "^4.2.13",
    "@types/mocha": "^8.2.0",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "@types/snowpack-env": "^2.3.2",
    "@web/test-runner": "^0.12.0",
    "chai": "^4.2.0",
    "css-loader": "^5.2.0",
    "prettier": "^2.0.5",
    "snowpack": "^3.0.1",
    "ts-loader": "^8.1.0",
    "typescript": "^4.0.0"
  }
}

What is the reason for this error and how can I solve it?

nimo23
  • 5,170
  • 10
  • 46
  • 75

3 Answers3

1

this is because using <React.StrictMode> remove <React.StrictMode> and try again

// saved in index.tsx

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(
    <App/>,
  document.getElementById('root'),
);

demo

John
  • 19
  • 4
  • Hi John. No, it's not because of ``. I tried it already before..removing does not help (I get the same error). – nimo23 Mar 31 '21 at 09:51
  • did you see my demo ? https://codesandbox.io/s/react-ui-forked-ymhl2 – John Mar 31 '21 at 09:53
  • yes, I see that demo and your demo works. I also copied it (to ensure that there is no type), however in my case, I get this error. Strange.. – nimo23 Mar 31 '21 at 10:00
  • 1
    I guess, the reason is that I use snowpack? – nimo23 Mar 31 '21 at 10:02
  • React version ? or Material UI version ? – John Mar 31 '21 at 10:03
  • I updated my question above (which includes the package.json). – nimo23 Mar 31 '21 at 10:04
  • @nimo23 I've started seeing this in an app that I'm attempting* to port to snowpack. I don't know what the relationship is, but it seems relevant. *(which is proving to be a major challenge & making me question all of the life choices that brought me here - not that I blame Snowpack for that, which looks like it **should** be good, but I'm really reaching the end of my tether here with a seemingly endless string of problems. Sorry, had to rant).* – PeterT May 05 '21 at 13:57
  • FWIW, in my case I'm seeing this with a material-ui ``. – PeterT May 05 '21 at 14:00
  • https://stackoverflow.com/questions/62935533/ho-to-fix-react-forwardrefmenu-material-ui is a similar issue, possibly with a solution. I didn't get `React.forwardRef` to work during my brief attempt, but changing the way I import to `import {Dialog} from '@material-ui/core'` which has worked around it for now while I chase down some more of my seemingly endless issues trying to get this snowpack config to work. – PeterT May 05 '21 at 15:07
1

The crucial part of the error is not the part **Check the render method of ForwardRef(BottomNavigationAction). **. This only tells you where to look for the error.

The important part of the error is:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

This usually happens when you don't pass a React component in the right way. I often faced this error when passing icon components in the wrong way.

For example instead of icon={<RestoreIcon />}, I would pass icon={RestoreIcon}. However, you seem to be passing the icons in the right manner.

The icon has to be a so-called ReactNode. It's Typescript type is:

type ReactNode = ReactElement | string | number | ReactFragment | ReactPortal | boolean | null | undefined;

So when getting passed an object it will through an error.

I share different ways in which you could pass a component:

  • icon={<RestoreIcon />}
  • icon={RestoreIcon}
  • icon={RestoreIcon()} will complain about missing props argument
  • icon={props => <RestoreIcon {...props} />}
Andru
  • 5,954
  • 3
  • 39
  • 56
-3

I don't know why. But try this:

import {
  BottomNavigation
  // your other import here
} from '@material-ui/core'
Aliar
  • 73
  • 7