简体   繁体   English

Material-UI 的自动完成组件不会导致任何渲染

[英]The Autocomplete component from Material-UI causes nothing to render

I have an Autocomplete component returned inside the render method of a class.我在 class 的渲染方法中返回了一个自动完成组件。 However, it causes nothing to render;但是,它不会导致任何渲染; taking the component out allows everything else to render as expected.取出组件允许其他所有内容按预期呈现。

export default class Home extends Component {
    render() {  
      return (
        ...
        <Row>
          <Autocomplete
            id="combo-box-demo"
            options={[{title: 'Inception'}, {title: 'Dark Knight'}]}
            getOptionLabel={option => option.title}
            style={{ width: 300 }}
            renderInput={params => (
               <TextField {...params} label="Combo box" variant="outlined" fullWidth />
            )}
          />
        </Row>
      );
    }
  }

Taking out Autocomplete and everything inside allows the render to work normally.取出自动完成和里面的所有东西都可以让渲染正常工作。 I am using the example Autocomplete code from Material-UI's guide.我正在使用 Material-UI 指南中的示例自动完成代码。

I receive this error in the console:我在控制台中收到此错误:

*Uncaught Error: Invalid hook call. *未捕获的错误:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.钩子只能在 function 组件的主体内部调用。 This could happen for one of the following reasons:这可能由于以下原因之一而发生:

  1. You might have mismatching versions of React and the renderer (such as React DOM)你可能有不匹配的 React 版本和渲染器(例如 React DOM)
  2. You might be breaking the Rules of Hooks您可能违反了 Hooks 规则
  3. You might have more than one copy of React in the same app*您可能在同一个应用程序中拥有多个 React 副本*

What could be causing this render issue?什么可能导致此渲染问题? What are the constraints on using Autocomplete(or hooks)?使用自动完成(或钩子)有什么限制?

Your code seems to work correctly.您的代码似乎工作正常。

Could you check the live demo that I have created down below?你能看看我在下面创建的现场演示吗?

Functional component: https://codesandbox.io/s/material-ui-rh82u?fontsize=14&hidenavigation=1&theme=dark功能组件: https://codesandbox.io/s/material-ui-rh82u?fontsize=14&hidenavigation=1&theme=dark

Class component: https://codesandbox.io/s/autocomplete-material-ui-tmtoj?fontsize=14&hidenavigation=1&theme=dark Class 组件: https://codesandbox.io/s/autocomplete-material-ui-tmtoj?fontsize=14&hidenavigation=1&theme=dark

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM