Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: color.charAt is not a function #16341

Closed
rofiuddin15 opened this issue Jun 23, 2019 · 20 comments · Fixed by #20253
Closed

TypeError: color.charAt is not a function #16341

rofiuddin15 opened this issue Jun 23, 2019 · 20 comments · Fixed by #20253
Labels
support: question Community support but can be turned into an improvement

Comments

@rofiuddin15
Copy link

I have some warning like this:

TypeError: color.charAt is not a function

decomposeColor
node_modules/@material-ui/core/esm/styles/colorManipulator.js:125

  122 |   return color;
  123 | }
  124 | 
> 125 | if (color.charAt(0) === '#') {
  126 |   return decomposeColor(hexToRgb(color));
  127 | }
  128 | 

lighten
node_modules/@material-ui/core/esm/styles/colorManipulator.js:270

Array.onUpdate
node_modules/jss-plugin-rule-value-function/dist/jss-plugin-rule-value-function.esm.js:46

@joshwooding joshwooding added the status: waiting for author Issue with insufficient information label Jun 23, 2019
@joshwooding
Copy link
Member

Please provide a reproducible example. I would also check your imports and make sure they are less than 2 path levels.

@zakir360hossain

This comment has been minimized.

@MoFarah9

This comment has been minimized.

@KitsonBroadhurst
Copy link
Contributor

For anyone else with this issue, I'd just done something dumb and assigned a primary text color like this:
text: { primary: { 500: '#467fcf' }, ... }
rather than this:
text: { primary: '#467fcf', ... }

@oliviertassinari oliviertassinari removed the status: waiting for author Issue with insufficient information label Feb 6, 2020
@oliviertassinari
Copy link
Member

oliviertassinari commented Feb 6, 2020

Thanks for the feedback. It sounds like we should be able to improve the error message :). The current one is cryptic.

@oliviertassinari oliviertassinari added ready to take Help wanted. Guidance available. There is a high chance the change will be accepted new feature New feature or request labels Feb 6, 2020
@mugungalabz
Copy link

The comments marked off-topic are what I used to solve this same issue.

@oliviertassinari
Copy link
Member

oliviertassinari commented Feb 28, 2020

@Mugungaman Do you have a reproduction for the error? How can we reproduce it?

@mugungalabz
Copy link

mugungalabz commented Feb 28, 2020

This recreates, where 'content' is a style applied to a component...

App.js:

const theme = createMuiTheme({
 ...,
  content: {
    padding: 25,
    objectFit: "cover"
  }
});

Child component:

const styles = theme => ({
  ...theme
});
<CardContent className={classes.content}>

...and this fixes it, wrapping the style name 'content' in a spreadIt object:

App.Js:

const theme = createMuiTheme({
...,
  spreadIt: {
    content: {
      padding: 25,
      objectFit: "cover"
    }
  }
});

Child component:

const styles = theme => ({
  ...theme.spreadIt
});
<CardContent className={classes.content}>

@oliviertassinari oliviertassinari added status: waiting for author Issue with insufficient information new feature New feature or request and removed ready to take Help wanted. Guidance available. There is a high chance the change will be accepted new feature New feature or request status: waiting for author Issue with insufficient information labels Feb 29, 2020
@oliviertassinari
Copy link
Member

I have no idea how developers are creating this issue. Let's close until we have a full reproduction.

@oliviertassinari oliviertassinari added status: waiting for author Issue with insufficient information and removed new feature New feature or request labels Feb 29, 2020
@vishal-vp
Copy link

vishal-vp commented Mar 5, 2020

@oliviertassinari I have reproduced this. Please check this sandbox https://codesandbox.io/s/material-uiissues16341-7mfwk

@vishal-vp
Copy link

vishal-vp commented Mar 6, 2020

I have figured it out. The problem was due to using the colors wrongly in theme. The following code causes the error.

const theme = createMuiTheme({
  palette: {
    primary: {
      main: purple,
    },
    secondary: {
      main: green,
    },
  },
  status: {
    danger: "orange"
  }
});

The correct way to use colors in theme is as follows: (Notice that value of primary should be the color and not the main key of primary object).

const theme = createMuiTheme({
  palette: {
    primary: purple,
    secondary: green,
  },
  status: {
    danger: "orange"
  }
});

To be honest the official documentation does not consolidate all the aspects of customizing theme. It is broken down into different pages. I guess it causes some confusion.

After getting familiar with the material-ui, I was able to spot this mistake.

@gota1993
Copy link

gota1993 commented Mar 6, 2020

"@material-ui/core": "^4.9.5",

I have same issue.

In App.js I have

const theme = createMuiTheme({
  palette: {
    primary: { main: '#3f50b5' },
    secondary: { main: '#f44336' },
  },
  customError: { color: 'red' }
});

In child component, I have

const styles = theme => ({ ...theme });
export default withStyles(styles)(Login);

and here is error

1
2

@gota1993
Copy link

gota1993 commented Mar 6, 2020

I have figured it out. The problem was due to using the colors wrongly in theme. The following code causes the error.

const theme = createMuiTheme({
  palette: {
    primary: {
      main: purple,
    },
    secondary: {
      main: green,
    },
  },
  status: {
    danger: "orange"
  }
});

The correct way to use colors in theme is as follows: (Notice that value of primary should be the color and not the main key of primary object).

const theme = createMuiTheme({
  palette: {
    primary: purple,
    secondary: green,
  },
  status: {
    danger: "orange"
  }
});

To be honest the official documentation does not consolidate all the aspects of customizing theme. It is broken down into different pages. I guess it causes some confusion.

After getting familiar with the material-ui, I was able to spot this mistake.

this doesn't fix issue for me

@vishal-vp
Copy link

@gota1993 Try to reproduce it at https://codesandbox.io/ and share it so that others can debug it.

@NelsonZeng25
Copy link

Top voted answer for this question fixed it for me.
https://stackoverflow.com/questions/56897838/getting-a-error-typeerror-color-charat-is-not-a-function-in-c-node-modul

@Taraass
Copy link

Taraass commented May 25, 2020

i just run npm install @material-ui/[email protected] and problem was solved

@bengbengku
Copy link

This recreates, where 'content' is a style applied to a component...

App.js:

const theme = createMuiTheme({
 ...,
  content: {
    padding: 25,
    objectFit: "cover"
  }
});

Child component:

const styles = theme => ({
  ...theme
});
<CardContent className={classes.content}>

...and this fixes it, wrapping the style name 'content' in a spreadIt object:

App.Js:

const theme = createMuiTheme({
...,
  spreadIt: {
    content: {
      padding: 25,
      objectFit: "cover"
    }
  }
});

Child component:

const styles = theme => ({
  ...theme.spreadIt
});
<CardContent className={classes.content}>

Thank you, its work!

@MOIN-AKHTAR
Copy link

MOIN-AKHTAR commented Dec 7, 2020

I was also facing this problem but problem solved when i put my custom styling in an object and other MUIStyling in a separate object such as palete and much more I have a login and signup form for which i have made styling and this styling is inside loginSignupStyle Object look at this example

{
    palette:{
      primary:{
         light:"#33C9DC",
         main:"#00BCD4",
         dark:"#008394",
         contrastText:"#fff"
      },
      secondary:{
        light:"#FF6333",
        main:"#FF3D00",
        dark:"#B22A00",
        contrastText:"#fff"
      }
    },
    loginSignupStyle:{
    image:{
        margin:"10px auto"
    },
   form:{
       textAlign:"center"
   },
   textFeild:{
       margin:"10px auto"
   },
   loginBtn:{
       marginTop:10,
       position:"relative"
   },
   errorText:{
       color:"red",
       fontSize:"0.8rem",
       display:"block",
       margin:"10px auto"
   },
   account:{
       display:"block",
       marginTop:10
   },
   loading:{
       position:"absolute"
   }
   }
}

It worked for me

@oliviertassinari oliviertassinari added the support: question Community support but can be turned into an improvement label Dec 7, 2020
@spacejamjim
Copy link

spacejamjim commented Jun 30, 2021

So after reading all of this it seems there is no way to have nested objects inside of a palette?

i.e. the following is impossible:

palette: {
  notifications: {
    error: {
      main: '#C42014',
      dark: '#CC4137',
      light: '#DB7A73',
    },
    success: {
      main: '#111',
      dark: '#222',
      light: '#333',
    },
  },
}

so I have to do the following ?

palette: {
    notificationError: {
        main: '#C42014',
        dark: '#CC4137',
        light: '#DB7A73',
    },
    notificationSuccess: {
        main: '#111',
        dark: '#222',
        light: '#333',
    },
}

@Janitha133
Copy link

I was also facing this problem but problem solved when i put my custom styling in an object and other MUIStyling in a separate object such as palete and much more I have a login and signup form for which i have made styling and this styling is inside loginSignupStyle Object look at this example

{
    palette:{
      primary:{
         light:"#33C9DC",
         main:"#00BCD4",
         dark:"#008394",
         contrastText:"#fff"
      },
      secondary:{
        light:"#FF6333",
        main:"#FF3D00",
        dark:"#B22A00",
        contrastText:"#fff"
      }
    },
    loginSignupStyle:{
    image:{
        margin:"10px auto"
    },
   form:{
       textAlign:"center"
   },
   textFeild:{
       margin:"10px auto"
   },
   loginBtn:{
       marginTop:10,
       position:"relative"
   },
   errorText:{
       color:"red",
       fontSize:"0.8rem",
       display:"block",
       margin:"10px auto"
   },
   account:{
       display:"block",
       marginTop:10
   },
   loading:{
       position:"absolute"
   }
   }
}

It worked for me

After that, get it in child component like this way

const styles = (theme) => ({
  ...theme.loginSignupStyle,
});

It worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support: question Community support but can be turned into an improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.