reactjs - 使用 react-select 但占位符未显示

标签 reactjs react-select

正如您在此沙盒上看到的那样,我已经完成了一些样式,但我不知道它的占位符文本。我不确定为什么会这样?
链接到 CodeSandbox:
https://codesandbox.io/s/nifty-rgb-ro8to?file=/src/components/Countries.js

export default function Countries({ formValues, setFormValues }) {
  const [data, setData] = useState([]);
  const [search, setSearch] = useState({ country: "" });
  const { register, handleSubmit } = useForm();

  // Fetch Data From Api
  useEffect(() => {
    const fetchData = () => {
      fetch("https://restcountries.eu/rest/v2/all")
        .then((res) => res.json())
        .then((result) => setData(result))
        .catch((err) => console.log("error"));
    };
    fetchData();
  }, []);

  const options = data.map((d) => ({
    label: d.name
  }));

  function handleChange(item) {
    setSearch(item);
  }

  function onSubmit(values) {
    setFormValues({
      ...formValues,
      ...values,
      ...search
    });
    console.log({ ...formValues, ...values, ...search });
  }

  // styles for the select
  const customStyles = {
    option: (provided, state) => ({
      ...provided,
      borderBottom: "1px solid #dede",
      color: state.isSelected ? "#53e3a6" : "green",
      backgroundColor: "white",
      padding: 10
    }),
    control: (base, state) => ({
      ...base,
      color: state.isSelected ? "#53e3a6" : "green",
      border: "1px solid rgba(255, 255, 255, 0.4)",
      boxShadow: "none",
      margin: 20,
      "&:hover": {
        border: "1px solid rgba(255, 255, 255, 0.4)"
      }
    }),
    placeholder: (base) => ({
      ...base,
      // backgroundColor: "black",
      fontSize: "2em",
      color: "black",
      fontWeight: 400
    })
  };

  return (
    <>
      <form onSubmit={handleSubmit(onSubmit)} className="search">
        <Select
          type="text"
          defaultValue={""}
          placeholder={"Search Country...."}
          value={search.country}
          onChange={handleChange}
          name="Search"
          ref={register}
          options={options}
          styles={customStyles}
        />

        <div>
          <button type="Submit">Submit</button>
        </div>
      </form>
    </>
  );
}

最佳答案

您将错误的状态传递给 value react-select的 Prop |
更改此行:

value={search}
到:
value={search.country}
原因:search是一个非虚假的状态对象,占位符仅在您传递虚假值时才会显示(例如,空字符串或 undefined)
现场演示
Edit 66867501/using-react-select-but-placeholder-is-not-showing

关于reactjs - 使用 react-select 但占位符未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66867501/

相关文章:

javascript - ReactJS - 无法读取新路由器未定义的属性 'location'

javascript - React-router-V4 未将 `match` 属性传递给渲染组件

css - 如何使 <Col> 内的文本垂直对齐 <img> 的大小? ( Ant 设计)

reactjs - 如何在 React.js 中正确设置 Azure Media Player?

reactjs - 自定义组件 "ValueContainer"的 React-Select 失去焦点

ReactJS ErrorBoundary 没有捕获 undefined variable

javascript - 有什么方法可以防止 react 选择(Select.Async)在失去焦点时删除搜索输入值?

react-select - 限制下拉列表中可见项目的数量

reactjs - 如何使用 react-bootstrap 组件和 react-select 菜单解决 z-index 问题?

reactjs - 使用 Typescript 的 React-Select 导入失败