Reactjs 需要根据传递的道具添加不同的样式

Reactjs 需要根据传递的道具添加不同的样式,reactjs,material-ui,Reactjs,Material Ui,我有一个select组件,需要根据传递的道具为选项添加一些额外的填充。这是因为在某些情况下,我有一个绝对位于选择组件内的图标,因此在这种情况下需要在左侧设置间距 render() const { classes, icon, selectWithIcon } = this.props; return( <Select value={this.state.value} onChange={this.handleChange} in

我有一个select组件,需要根据传递的道具为选项添加一些额外的填充。这是因为在某些情况下,我有一个绝对位于选择组件内的图标,因此在这种情况下需要在左侧设置间距

render()
const { classes, icon, selectWithIcon } = this.props;

return(
<Select
          value={this.state.value}
          onChange={this.handleChange}
          input={<FilledInput 
          name={this.props.label} 
          id={this.props.id} />}
        >
        {this.props.options && this.props.options.map(option => ( 
          <MenuItem value={option.value} >
            {<ListItemText className={classes.listItemText} primary={<Typography style={{ color: option.color, {selectWithIcon? marginLeft: '10px' : ''}}}>{option.text}</Typography>}/>}
          </MenuItem>
        ))}
        </Select>
)
render()
const{classes,icon,selectWithIcon}=this.props;
返回(
{this.props.options&&this.props.options.map(option=>)
{}
))}
)
如何在菜单项中添加条件来设置主菜单项的样式?上面的代码给了我一个错误

以下是正确的语法(您需要将条件放在
marginLeft
值中,而不是放在样式对象中的键周围):


{option.text}
下面是一个完整的工作示例:

import React from "react";
import ReactDOM from "react-dom";

import Select from "@material-ui/core/Select";
import MenuItem from "@material-ui/core/MenuItem";
import ListItemText from "@material-ui/core/ListItemText";
import Typography from "@material-ui/core/Typography";

const MySelect = ({ selectWithIcon, options }) => {
  return (
    <Select value="1">
      {options.map(option => (
        <MenuItem value={option.value}>
          {
            <ListItemText
              primary={
                <Typography
                  style={{
                    color: option.color,
                    marginLeft: selectWithIcon ? "10px" : ""
                  }}
                >
                  {option.text}
                </Typography>
              }
            />
          }
        </MenuItem>
      ))}
    </Select>
  );
};
function App() {
  const options = [
    { text: "Item 1", value: 1, color: "blue" },
    { text: "Item 2", value: 2, color: "purple" }
  ];
  return (
    <div className="App">
      <MySelect options={options} selectWithIcon={true} />
      <br />
      <MySelect options={options} selectWithIcon={false} />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
从“React”导入React;
从“react dom”导入react dom;
从“@material ui/core/Select”导入选择;
从“@material ui/core/MenuItem”导入菜单项;
从“@material ui/core/ListItemText”导入ListItemText;
从“@material ui/core/Typography”导入排版;
const MySelect=({selectWithIcon,options})=>{
返回(
{options.map(option=>(
{
}
))}
);
};
函数App(){
常量选项=[
{文本:“项目1”,值:1,颜色:“蓝色”},
{文本:“项目2”,值:2,颜色:“紫色”}
];
返回(

); } const rootElement=document.getElementById(“根”); ReactDOM.render(

import React from "react";
import ReactDOM from "react-dom";

import Select from "@material-ui/core/Select";
import MenuItem from "@material-ui/core/MenuItem";
import ListItemText from "@material-ui/core/ListItemText";
import Typography from "@material-ui/core/Typography";

const MySelect = ({ selectWithIcon, options }) => {
  return (
    <Select value="1">
      {options.map(option => (
        <MenuItem value={option.value}>
          {
            <ListItemText
              primary={
                <Typography
                  style={{
                    color: option.color,
                    marginLeft: selectWithIcon ? "10px" : ""
                  }}
                >
                  {option.text}
                </Typography>
              }
            />
          }
        </MenuItem>
      ))}
    </Select>
  );
};
function App() {
  const options = [
    { text: "Item 1", value: 1, color: "blue" },
    { text: "Item 2", value: 2, color: "purple" }
  ];
  return (
    <div className="App">
      <MySelect options={options} selectWithIcon={true} />
      <br />
      <MySelect options={options} selectWithIcon={false} />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);