我想用Autocomplete个组件作为输入标签.我正在try 获取标签并将其保存到一个状态,以便以后可以将其保存到数据库中.我在react中使用函数而不是类.我try 了onChange,但没有得到任何结果.

<div style={{ width: 500 }}>
  <Autocomplete
    multiple
    options={autoComplete}
    filterSelectedOptions
    getOptionLabel={(option) => option.tags}
    renderInput={(params) => (
      <TextField
        className={classes.input}
        {...params}
        variant="outlined"
        placeholder="Favorites"
        margin="normal"
        fullWidth
      />
    )}
  />
</div>;

推荐答案

正如Yuki已经提到的,确保您正确使用了onChange函数.它接收两个参数.根据文件:

Signature: function(event: object, value: any) => void.

event:回调的事件源

value:null(自动完成组件中的值).

下面是一个例子:

import React from 'react';
import Chip from '@material-ui/core/Chip';
import Autocomplete from '@material-ui/lab/Autocomplete';
import TextField from '@material-ui/core/TextField';

export default class Tags extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      tags: []
    };
    this.onTagsChange = this.onTagsChange.bind(this);
  }

  onTagsChange = (event, values) => {
    this.setState({
      tags: values
    }, () => {
      // This will output an array of objects
      // given by Autocompelte options property.
      console.log(this.state.tags);
    });
  }

  render() {
    return (
      <div style={{ width: 500 }}>
        <Autocomplete
          multiple
          options={top100Films}
          getOptionLabel={option => option.title}
          defaultValue={[top100Films[13]]}
          onChange={this.onTagsChange}
          renderInput={params => (
            <TextField
              {...params}
              variant="standard"
              label="Multiple values"
              placeholder="Favorites"
              margin="normal"
              fullWidth
            />
          )}
        />
      </div>
    );
  }
}

const top100Films = [
  { title: 'The Shawshank Redemption', year: 1994 },
  { title: 'The Godfather', year: 1972 },
  { title: 'The Godfather: Part II', year: 1974 },
  { title: 'The Dark Knight', year: 2008 },
  { title: '12 Angry Men', year: 1957 },
  { title: "Schindler's List", year: 1993 },
  { title: 'Pulp Fiction', year: 1994 },
  { title: 'The Lord of the Rings: The Return of the King', year: 2003 },
  { title: 'The Good, the Bad and the Ugly', year: 1966 },
  { title: 'Fight Club', year: 1999 },
  { title: 'The Lord of the Rings: The Fellowship of the Ring', year: 2001 },
  { title: 'Star Wars: Episode V - The Empire Strikes Back', year: 1980 },
  { title: 'Forrest Gump', year: 1994 },
  { title: 'Inception', year: 2010 },
];

Reactjs相关问答推荐

React onKeyUp不一致地使用快速Shift+键按压

两条react 路由的行为不同.有没有人能解释一下,我已经找了好几天了

将对象添加到集合中的数组时的no_id字段

为什么我的react虚拟化列表不显示滚动条,除非我确保高度低于rowCount*rowHeight?

使用Next.js和Next-intl重写区域设置

useEffect firebase 清理功能如何工作?

React 在第一次渲染时为 null

如何在react 中过滤数组的数组?

验证 Yup 中的对象项

useEffect内部的RETURN语句在首次按钮点击事件中似乎无效的原因是什么?

我收到Uncaught TypeError: props.handleSelect is not a function

需要先填写依赖输入字段,以便其他人工作

MUI - ButtonGroup fullWidth 条件屏幕大小不起作用?

将 ref 转发到所有页面(路由组件)只是为了将其应用于
并具有跳过导航链接(按钮).有没有更好的办法?

react 事件顺序

React Native - 身份验证 - 触发值以更改 Auth 和 UnAuth 堆栈导航器

如何在 react-router-dom v6 中实现监听功能?

您无权使用阻止 webRequest 侦听器.请务必在 list 中声明 webRequestBlocking 权限

警告:您在没有 `onChange` 处理程序的情况下为表单字段提供了 `value` 属性

根据计算 ReactJs 更新输入字段