简体   繁体   English

反应 Material-ui Select

[英]React Material-ui Select

After i implemented my application, i wanted to style it with Material UI.在我实现了我的应用程序之后,我想用 Material UI 来设置它的样式。 When changing from html to Material i started recieving a warning when clicking select item:当从 html 更改为 Material 时,单击 select 项目时我开始收到警告:

index.js:1 Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of Transition which is inside StrictMode. Instead, add a ref directly to the element you want to reference.

Initial code:初始代码:

<label htmlFor="newRole">Role</label>
        <select
          name="newRole"
          id="newRole"
          value={newUser.newRole}
          onChange={handleChangeNew}
        >
          <option value="0">Student</option>
          <option value="2">Professor</option>
          <option value="3">Secretary</option>
        </select>

Material UI code:材质界面代码:

 <InputLabel shrink id="role-lbl"> Role </InputLabel>
            <Select
              labelId="role-lbl"
              name="newRole"
              id="newRole"
              value={newUser.newRole}
              onChange={handleChangeNew}
              // displayEmpty
              variant="filled"
            >
              <MenuItem value="0">Student</MenuItem>
              <MenuItem value="2">Professor</MenuItem>
              <MenuItem value="3">Secretary</MenuItem>
            </Select>

That is a little bit strange but I can not reproduce your warning.这有点奇怪,但我无法重现您的警告。 But as I faced it several times I can give you two advices how to fix it.但是当我多次面对它时,我可以给你两个建议来解决它。

  1. (not recommend) Just remove <React.StrictMode> wrapper on your <App /> (不推荐)只需删除<App />上的<React.StrictMode>包装器
  2. useRefs .使用参考 The idea might look like this:这个想法可能看起来像这样:
import {InputLabel, MenuItem, Select} from "@material-ui/core";
import {useRef} from "react";

export default function Example3() {
    const nodeRef = useRef(null);
    
    function handleChangeNew(){
        console.log('changed');
    }

    return(
        <>
          <InputLabel shrink id="role-lbl"> Role </InputLabel>
            <Select
                noderef={nodeRef}
                labelId="role-lbl"
                id="newRole"
                value={newUser.newRole}
                onChange={handleChangeNew}
                // displayEmpty
                variant="filled"
            >
                <MenuItem value="0" ref={nodeRef}>Student</MenuItem>
                <MenuItem value="1" ref={nodeRef}>Professor</MenuItem>
                <MenuItem value="2" ref={nodeRef}>Secretary</MenuItem>
            </Select>
        </>
    )
}

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

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