Skip to content

makeStyles, withTheme and useTheme not working #14416

Description

@ericsolberg

I'm trying to use the sample code for an app bar provided in the documentation (note that I'm not using the AppBar component- I have my own header component. But I'm currently trying to create a SearchField component based on this code). I've basically cut and pasted the code as-is, and pulled out the parts not related to the search field. I'm getting an error: Uncaught TypeError: Cannot read property 'borderRadius' of undefined. Digging into it, I see that the theme object being passed into the function I pass into makeStyles is an empty object. I investigated further, to see if I could get my hands on the theme. When I export my component with withTheme()(searchBar), the theme object in the props is null. Likewise, if I just call useTheme() in my component, it also returns null.

Note: it seems the plain JS sample code (no hooks) works - so this has something to do with the Material-UI and React 16.8.0-alpha.1. However- this version is required to use the sample hooks code.

  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 🤔

My makeStyles function should be passed the populated theme object, and withTheme and useTheme should return the proper non-null theme object.

Current Behavior 😯

The theme object passed into makeStyles is empty. The theme props attribute injected by withTheme is null. The theme object returned by useTheme is null.

Steps to Reproduce 🕹

Link: I don't have a live link but it's easy to reproduce:

  1. create-react-app testy
  2. cd testy
  3. Modify package.json, setting the react and react-dom versions to ^16.8.0-alpha.1 (to make use of hooks)
    "react": "^16.8.0-alpha.1",
    "react-dom": "^16.8.0-alpha.1",
  1. npm install (to update react)
  2. npm install --save @material-ui/core @material-ui/styles @material-ui/icons
  3. Copy the Hooks version of the Sample App Bar with a primary search field
  4. Paste this code into the test app's /src/App.js file (overwrite the existing code) and save
  5. npm start - you will get an undefined error when the makeStyles function accesses the empty theme object

Context 🔦

I'm trying to implement a toolbar in my app with Material-UI.

Your Environment 🌎

Tech Version
Material-UI v3.9.2
React 16.8.0-alpha.1
Browser Chrome 71.0
TypeScript 3.3.0-rc
etc.

Metadata

Metadata

Labels

docsImprovements or additions to the documentation.priority: importantThis change can make a difference.type: bugIt doesn't behave as expected.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions