<FormControl />不接受变体Prop

时间:2019-10-10 11:40:19

标签: javascript reactjs material-ui

我想知道为什么<FormControl />不接受道具variant。根据{{​​3}},此道具可用。

import React from "react";
import { render } from "react-dom";
import FormControl from "@material-ui/core/FormControl";
import Select from "@material-ui/core/Select";
import MenuItem from "@material-ui/core/MenuItem";

const App = () => {
  return (
    <div>
      <FormControl variant="outlined">
        <Select value="foo" onChange={() => {}} autoWidth={true}>
          <MenuItem value="foo">Kitten</MenuItem>
          <MenuItem value="bar">Puppy</MenuItem>
        </Select>
      </FormControl>
    </div>
  );
};

render(<App />, document.getElementById("root"));

但是,它不起作用。这是一个沙盒重现该问题。 docs

1 个答案:

答案 0 :(得分:0)

import React, { useState } from "react";
import { render } from "react-dom";
import FormControl from "@material-ui/core/FormControl";
import Select from "@material-ui/core/Select";
import MenuItem from "@material-ui/core/MenuItem";

const App = () => {
  const [state, setValues] = useState({
    defaults: "foo"
  });

  const changeValue = e => {
    setValues({ ...state, defaults: e.target.value });
  };
  return (
    <div>
      <FormControl variant="outlined">
        <Select
          value={state.defaults}
          onChange={e => {
            changeValue(e);
          }}
          autoWidth={true}
        >
          <MenuItem value="foo">Kitten</MenuItem>
          <MenuItem value="bar">Puppy</MenuItem>
        </Select>
      </FormControl>
    </div>
  );
};

render(<App />, document.getElementById("root"));

package.json

{
  "dependencies": {
     "react": "^16.7.0-alpha.2",
     "react-dom": "^16.7.0-alpha.2",
    ...
  },

https://codesandbox.io/s/material-ui-ksggx?fontsize=14

尝试一下,它将起作用