使用 Material UI 中的 Dropdown 组件在 React JS 中设置和更新状态

健内中

我有一段代码,它调用 Web 服务并将来自该 WS 的名称显示到 Material UI 的 Dropdown 组件中,

我想要做的是使用来自 WS 的第一个元素设置下拉列表的默认值,并且还能够选择下拉列表中的任何选项,我读了一些关于“状态”的内容,但没有得到它真的很好在代码级别。

我是 React 的新手并且自己学习,但一些帮助会很好。

    import React, { Component } from 'react';
    import DropDownMenu from 'material-ui/DropDownMenu';
    import MenuItem from 'material-ui/MenuItem';

    export default class WebserviceTest extends Component {

      constructor(props) {
        super(props);
        this.state = {
        data: [],
       };
       this.renderOptions = this.renderOptions.bind(this);
     }

     componentDidMount() {
     const url = 'https://randomuser.me/api/?results=4';

     fetch(url)
       .then(Response => Response.json())
       .then(findResponse => {
         console.log(findResponse);
         this.setState({
           data: findResponse.results
         });
       });
    }
    //will set wahtever item the user selects in the dropdown
    handleChange(event, index, value) {this.setState({ value });}
    //we are creating the options to be displayed
    renderOptions() {
     return this.state.data.map((dt, i) => {
       return (
         <div key={i}>
           <MenuItem
             label="Select a description"
             value={dt.name.first}
             primaryText={dt.name.first} />
         </div>
       );
     });
    }

    render() {
     return (
       <div>
         <DropDownMenu value={this.state.name} onChange={this.handleChange}>
           {this.renderOptions()}
         </DropDownMenu>
       </div>
     );
   }
  }

提前致谢。

问候。

神仙

您需要设置所选下拉选项的状态。并将数据的第一个值设置为选定值。

componentDidMount() {
 const url = 'https://randomuser.me/api/?results=4';

 fetch(url)
   .then(Response => Response.json())
   .then(findResponse => {
     console.log(findResponse);
     this.setState({
       data: findResponse.results,
       selected: findResponse.results[0].name.first // need to be sure it's exist
     });
   });
}
handleChange(event, index, value) {this.setState({ selected: value });}
.
.
.
render() {
 return (
   <div>
     <DropDownMenu value={this.state.selected} onChange={this.handleChange}>
       {this.renderOptions()}
     </DropDownMenu>
   </div>
 );

}

更新代码

import React, { Component } from 'react';
import DropDownMenu from 'material-ui/DropDownMenu';
import MenuItem from 'material-ui/MenuItem';

export default class WebserviceTest extends Component {
  constructor() {
    super();
    this.state = {
      data: [],
      selected: '',
    };
    this.renderOptions = this.renderOptions.bind(this);
    this.handleChange = this.handleChange.bind(this);
  }

  componentDidMount() {
    const url = 'https://randomuser.me/api/?results=4';

    fetch(url)
      .then(Response => Response.json())
      .then(findResponse => {
        console.log(findResponse);
        console.log(findResponse.results[0].name.first);
        this.setState({
          data: findResponse.results,
          selected: findResponse.results[0].name.first // need to be sure it's exist
        });
      });
  }
  handleChange(value) {this.setState({ selected: value });}

  //we are creating the options to be displayed
  renderOptions() {
    return this.state.data.map((dt, i) => {
      return (
        <div key={i}>
          <MenuItem
            value={dt.name.first}
            primaryText={dt.name.first} />
        </div>
      );
    });
  }

  render() {
    return (
      <div>
        <DropDownMenu value={this.state.selected} onChange={this.handleChange}>
          {this.renderOptions()}
        </DropDownMenu>
      </div>
    );
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Material-UI从React中的组件增强

在表单中如何使用Material UI的React评分组件?

React + Material UI-使用样式覆盖组件中的MuiTheme

在React中设置Material UI的TextField组件的defultValue

如何在 React Material UI 的 AppBar 组件中设置容器?

使用 React.js 和 Material-UI 将数据渲染为功能组件

material-ui 包的 AppBar 组件在我的 react js 应用程序中不起作用

React中Material-UI中的可滚动列表组件

如何使用 Material-UI AppBar onTitleClick 属性在 React 中渲染新组件?

Material UI中缺少组件

将Material-ui React组件居中于页脚元素中

React onDragStart在Material UI Autocomplete组件中不触发

在 Material-UI List for React 中使用从 React 组件状态异步获取的 ListItem 组件

React JS在功能组件中设置状态

观察子组件React与Material UI的状态

在React.js Material-UI中设置BottomNavigation的样式

如何在Material UI中的Avatar组件上使用阴影

Material UI DatePicker 在 React Js 中不显示更新时间

将material-ui中的<TextField />组件与react-hook-form一起使用会显示警告

使用Material UI在React.js中发送带有道具的多个组件

如何使用React JS在Material UI中使整个Card组件可点击?

在 React 组件中动态使用 [lng].js

React.js:使用 material-ui 在表格中添加动作图标(编辑、删除)

如何从Material UI TextField React组件中删除弯曲的边框和插入框阴影?

如何在React和Material UI中访问更改组件内部项目的样式?

如何根据Material UI(React JS)的要求制作“选择”组件

React js Material UI Switch组件在循环中

如何根据Material UI(React JS)的要求制作“选择”组件

我无法使用样式化组件在Material-UI中居中放置Button组件