Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple React Error: Can't use @material-ui/picker library in Next.js

I'm trying to use the @material-ui/pickers library within a Next.js app.

I added the _app.js file inside /pages as indicated in the Next.js docs as a place to put the Picker provider:

import React from "react";
import PropTypes from "prop-types";
import { ThemeProvider } from "@material-ui/core/styles";
import { MuiPickersUtilsProvider } from "@material-ui/pickers";
import DateFnsUtils from "@date-io/date-fns"; // using version 1.x as described in the docs
import CssBaseline from "@material-ui/core/CssBaseline";
import theme from "../src/theme";

export default function MyApp(props) {
  const { Component, pageProps } = props;

  React.useEffect(() => {
    // Remove the server-side injected CSS.
    const jssStyles = document.querySelector("#jss-server-side");
    if (jssStyles) {
      jssStyles.parentElement.removeChild(jssStyles);
    }
  }, []);

  return (
    <React.Fragment>
      <ThemeProvider theme={theme}>
        <CssBaseline />
        <MuiPickersUtilsProvider utils={DateFnsUtils}>
          <div>Hello</div>
          <Component {...pageProps} />
        </MuiPickersUtilsProvider>
      </ThemeProvider>
    </React.Fragment>
  );
}

Code Sandbox example:
https://codesandbox.io/s/crimson-rgb-cqg5p?file=/pages/_app.js

The server won't start up with this setup for some reason. When doing a SPA react app (without Next.js), it works fine. Adding Next.js though causes issues. Any help is appreciated.

like image 300
qarthandso Avatar asked Dec 05 '25 08:12

qarthandso


1 Answers

You are missing a peer dependency. The @material-ui/pickers documentation could be better, but you need both @date-io/[email protected] and [email protected] dependencies (at least if you're going to use date-fns)

Try running the following and restarting your dev server:

npm install [email protected]
like image 70
rfestag Avatar answered Dec 07 '25 21:12

rfestag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!