简体   繁体   English

TypeError:无法读取未定义的属性(读取“主”)

[英]TypeError: Cannot read properties of undefined (reading 'main')

I'm getting above error when I try to run my below code... before using Material-UI code was working fine.. I need color radio buttons so I added Material-UI into my project.当我尝试运行下面的代码时出现上述错误......在使用 Material-UI 代码工作正常之前......我需要彩色单选按钮,所以我将 Material-UI 添加到我的项目中。 In terminal its compiling successfully but not able to run in a browser.在终端中编译成功,但无法在浏览器中运行。 Any type of help will be appreciate任何类型的帮助将不胜感激

import Radio from '@mui/material/Radio';

const GeneralComponent = () => {

  
  const [selectedValue, setSelectedValue] = React.useState('a');


  const handleChange = (event) => {
    setSelectedValue(event.target.value);
  };

  const controlProps = (item) => ({
    checked: selectedValue === item,
    onChange: handleChange,
    value: item,
    name: 'color-radio-button-demo',
    inputProps: { 'aria-label': item },
  });
return(

 <Radio  {...controlProps('a')} color="success" />
      <Radio {...controlProps('b')} color="yellow" />
      <Radio {...controlProps('c')} color="red" />
      <Radio {...controlProps('d')} color="default" />
      <Radio
        {...controlProps('e')}
        sx={{
          color: pink[800],
          '&.Mui-checked': {
            color: pink[600],
          },
        }}
      />

);
}

But I solved it by myself we can pass invalid (any color) colors too... but not directly in the below way但我自己解决了,我们也可以通过无效(任何颜色)colors ......但不能直接通过以下方式

{...controlProps('e')}
        sx={{
          color: anycolor[800],
          '&.Mui-checked': {
            color: anycolor[600],
          },
        }}

in this way这样

'red' , 'yellow' or 'default' are not valid color values of the Radio . 'red''yellow''default'不是Radio的有效color值。 The prop can only accept the following value by default: prop 默认只能接受以下值:

'default' | '默认' | 'primary' | '主要' | 'secondary' | '次要' | 'error' | '错误' | 'info' | '信息' | 'success' | '成功' | 'warning' | '警告' | string细绳

For reference, see the Radio API here .如需参考,请参阅此处Radio API。 If you want to extend the color, see this answer, it applies to all MUI components with the color prop.如果要扩展颜色,请参阅答案,它适用于所有具有color属性的 MUI 组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReactJS TypeError:无法读取未定义的属性(读取“main”) - ReactJS TypeError: Cannot read properties of undefined (reading 'main') × 类型错误:无法读取未定义的属性(读取“推送”) - × TypeError: Cannot read properties of undefined (reading 'push') 类型错误:无法读取未定义的属性(读取“过滤器”) - TypeError: Cannot read properties of undefined (reading 'filter') 类型错误:无法读取未定义的属性(读取“onSnapshot”) - TypeError: Cannot read properties of undefined (reading 'onSnapshot') 未捕获的类型错误:无法读取未定义的属性(读取“然后”)? - Uncaught TypeError: Cannot read properties of undefined (reading 'then')? TypeError:无法读取未定义的属性(读取“toLocaleString”) - TypeError: Cannot read properties of undefined (reading 'toLocaleString') TypeError:无法读取未定义的属性(读取“setRestaurants”) - TypeError: Cannot read properties of undefined (reading 'setRestaurants') TypeError:无法读取未定义的属性(读取“国家”) - TypeError: Cannot read properties of undefined (reading 'country') TypeError:无法读取未定义的属性(读取“然后”) - TypeError: Cannot read properties of undefined (reading 'then') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM