如何将数组传递给 material-ui dataSource 道具

How to pass an array to material-ui dataSource props

我正在使用 material-ui 作为组件。 material-ui 中有一个自动完成组件,我想用它来显示带有图标的图标名称列表。如果我只将 MenuItem 传递给数据源,我会得到一个没有列表的空输入框。如果我尝试像在我的代码中那样传递键和值,我会收到意外令牌错误。

这是我的代码

console.log('this.props.fetchIcon', this.props.fetchIcon.icons);
const listOfIcon = _.map(this.props.fetchIcon.icons, (icon) => {
                      return (text: {icon.name}, value: (<MenuItem primaryText={icon.name} secondaryText={icon.name} />));
                    });
return (
  <div className="device-action">
    <Dialog
        title="Add a Tab"
        actions={actions}
        modal={false}
        open={this.props.createTab.open}
        onRequestClose={this.props.CancelDeviceEvent}
    >
    <div className="icon">
      <AutoComplete
        floatingLabelText="select any icon"
        filter={AutoComplete.noFilter}
        openOnFocus
        dataSource={listOfIcon}
      />
    </div>
    </Dialog>
  </div>
);

减速机

const initialState = {
  fetching: false,
  fetched: true,
  icons: [],
  error: null,
};

export const fetchIconReducer = (state = initialState, action) => {
  switch (action.type) {
    case 'FETCH_ICONS_START':
      return { ...state, fetching: true };
    case 'FETCH_ICONS_ERROR':
      return { ...state, fetching: false, error: action.payload };
    case 'RECIEVE_ICONS':
      return { ...state, fetching: false, fetched: true, icons: action.payload };
    default:
      return state;
  }
};

this.props.fetchIcon.icons 控制台关注

您应该return dataSource 的对象数组。尝试以下..

_.map(this.props.fetchIcon.icons, (icon) => {
    return {
      text: icon.name,
      value: <MenuItem primaryText={icon.name} secondaryText={icon.name} />
    }
});