Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

material-ui-pickers example: TypeError: Object(...) is not a function

I am trying to set up the most basic possible material UI pickers example using a bare project and following the installation and usage steps described here: https://material-ui-pickers.dev/

I get the following error:

TypeError: Object(...) is not a function
Module../node_modules/@material-ui/pickers/dist/material-ui-pickers.esm.js
node_modules/@material-ui/pickers/dist/material-ui-pickers.esm.js:25
  22 | import Tab from '@material-ui/core/Tab';
  23 | import Tabs from '@material-ui/core/Tabs';
  24 | import Paper from '@material-ui/core/Paper';
> 25 | var useStyles = makeStyles(function (theme) {
  26 |   var textColor = theme.palette.type === 'light' ? theme.palette.primary.contrastText : theme.palette.getContrastText(theme.palette.background.default);
  27 |   return {
  28 |     toolbarTxt: {

The steps I followed are:

create-react-app material-ui-pickers-demo
yarn add @material-ui/core
yarn add @material-ui/pickers
yarn add @date-io/date-fns
import React, { useState } from "react";
import DateFnsUtils from "@date-io/date-fns"; // choose your lib
import {
  DatePicker,
  TimePicker,
  DateTimePicker,
  MuiPickersUtilsProvider,
} from "@material-ui/pickers";

function App() {
  const [selectedDate, handleDateChange] = useState(new Date());

  return (
    <MuiPickersUtilsProvider utils={DateFnsUtils}>
      <DatePicker value={selectedDate} onChange={handleDateChange} />
      <TimePicker value={selectedDate} onChange={handleDateChange} />
      <DateTimePicker value={selectedDate} onChange={handleDateChange} />
    </MuiPickersUtilsProvider>
  );
}

export default App;
yarn start

yarn version is 1.16.0 node version is v12.1.0

edit: I have seen this question and it did not answer mine Uncaught TypeError: Object(...) is not a function when importing @material-ui/pickers

like image 825
Batters Avatar asked May 10 '19 15:05

Batters


1 Answers

The cause is a wrong version of @material-ui/core. @material-ui/pickers works fine only with @material-ui/[email protected] for now. @material-ui/core beta 1 and @material-ui/pickers is too raw for using them in projects. So, to fix your problem you need following steps:

Remove node_modules directory and run yarn add @material-ui/[email protected] and finally run @material-ui/[email protected] It should fix the error.

like image 168
Ivan Wong Avatar answered Oct 18 '22 20:10

Ivan Wong