react-select的默认值与props不起作用

huangapple go评论109阅读模式
英文:

react-select default value did not work with props

问题

标题中提到的问题。我一直在使用react-select组件,并将属性传递给它作为默认值,但它没有显示。为什么会发生这种情况?即使我像这样将字符串传递给它:defaultValue="aa"

这是我的代码沙箱链接:
点击此处进入链接

英文:

As in the title. I have been used the react-select component and I pass props to it as the default value. but it did not show. why did this happen? even when I pass string to it like this defaultValue="aa"

this is the sandbox for my code:
enter link description here

答案1

得分: 1

defaultValue 应该是 Option 类型,请参阅 StateManager Props

const options = [
  { value: 'chocolate', label: 'Chocolate' },
  { value: 'strawberry', label: 'Strawberry' },
  { value: 'vanilla', label: 'Vanilla' },
];

// 应该是
<SelectItemComp b={options[0]} options={options} />

// 不应该是
<SelectItemComp b='chocolate' options={options} />

codesandbox

您可以在官方示例中找到更多信息 home#getting-started

英文:

The defaultValue should be the Option type, see StateManager Props

const options = [
  { value: &#39;chocolate&#39;, label: &#39;Chocolate&#39; },
  { value: &#39;strawberry&#39;, label: &#39;Strawberry&#39; },
  { value: &#39;vanilla&#39;, label: &#39;Vanilla&#39; },
];

// it should be
&lt;SelectItemComp b={options[0]} options={options} /&gt;

// not
&lt;SelectItemComp b=&#39;chocolate&#39; options={options} /&gt;

codesandbox

You can find the official example home#getting-started

答案2

得分: 0

import React, { useState } from "react";
import Select from "react-select";

const SelectItemComp = (props) => {
  const { b, options } = props;
  console.log(props);

  const [selectedItem, setSelectedItem] = useState(b);
  console.log(selectedItem);

  const handleChange = (e) => {
    const selectedItem = e.value;
    setSelectedItem(selectedItem);
    console.log("default", selectedItem);
  };

  return (
    <div>
      <Select onChange={handleChange} options={options} placeholder={selectedItem} />
    </div>
  );
};

export default SelectItemComp;

注意:上述代码为你提供的代码的翻译,不包含额外的内容。

英文:
 import React, { useState } from &quot;react&quot;;
 import Select from &quot;react-select&quot;;

  const SelectItemComp = (props) =&gt; {
   const { b, options } = props;
   console.log(props);
   
 
   const [selectedItem, setSelectedItem] = useState(b);
   console.log(selectedItem);
 
 
 
   const handleChange = (e) =&gt; {
     const selectedItem = e.value;
     setSelectedItem(selectedItem);
     console.log(&quot;default&quot;, selectedItem);
   };
 
   return (
     &lt;div&gt;
       &lt;Select onChange={handleChange} options={options}  placeholder={selectedItem} /&gt;
     &lt;/div&gt;
   );
 };
 
 export default SelectItemComp;

codesandbox react-select

答案3

得分: -1

请检查沙盒。我已经做了一些更改,可能会对你有所帮助。

import SelectItemComp from "./SelectItemComp";
import "./styles.css";

export default function App() {
  const options = [
    { id: 0, value: "default", label: "选择一个选项" },
    { id: 1, value: "chocolate", label: "巧克力" },
    { id: 2, value: "strawberry", label: "草莓" },
    { id: 3, value: "vanilla", label: "香草" }
  ];

  return (
    <div className="App">
      <SelectItemComp options={options} />
    </div>
  );
}

import React, { useState } from "react";
import Select from "react-select";

const SelectItemComp = (props) => {
  const { options } = props;
  const [selectedItem, setSelectedItem] = useState(options[0]);

  const handleChange = (e) => {
    const selectedItem = e.value;
    setSelectedItem(selectedItem);
  };

  console.log("默认值", selectedItem);

  return (
    <Select
      defaultValue={options[0]}
      onChange={handleChange}
      options={options}
    />
  );
};

// 如果你忘记将选项作为props传递给SelectItemComp组件,这可能会对你有所帮助
// 祝编程愉快
SelectItemComp.defaultProps = {
  options: [{ id: 0, value: "default", label: "选择一个选项" }]
};

export default SelectItemComp;

你需要在选项数组中包含默认值。你不必单独传递它作为props。为了简化起见,我添加了默认props,以防你忘记将选项props传递给SelectItemComp组件。

英文:

Please Check the sandbox. I've made some changes might help you.

import SelectItemComp from &quot;./SelectItemComp&quot;;
import &quot;./styles.css&quot;;

export default function App() {
  const options = [
    { id: 0, value: &quot;default&quot;, label: &quot;Select an option&quot; },
    { id: 1, value: &quot;chocolate&quot;, label: &quot;Chocolate&quot; },
    { id: 2, value: &quot;strawberry&quot;, label: &quot;Strawberry&quot; },
    { id: 3, value: &quot;vanilla&quot;, label: &quot;Vanilla&quot; }
  ];

  return (
    &lt;div className=&quot;App&quot;&gt;
      &lt;SelectItemComp options={options} /&gt;
    &lt;/div&gt;
  );
}

import React, { useState } from &quot;react&quot;;
import Select from &quot;react-select&quot;;

const SelectItemComp = (props) =&gt; {
  const { options } = props;
  const [selectedItem, setSelectedItem] = useState(options[0]);

  const handleChange = (e) =&gt; {
    const selectedItem = e.value;
    setSelectedItem(selectedItem);
  };

  console.log(&quot;default&quot;, selectedItem);

  return (
    &lt;Select
      defaultValue={options[0]}
      onChange={handleChange}
      options={options}
    /&gt;
  );
};

// This might help you if you don&#39;t pass any options as props to SelectItemComp
// Happy coding
SelectItemComp.defaultProps = {
  options: [{ id: 0, value: &quot;default&quot;, label: &quot;Select an option&quot; }]
};

export default SelectItemComp;

You need to include default value in the options array. You don't have to passe it separately as a prop. and for simplicity i added default props if you forgot to pass options props as param to SelectItemComp component .

huangapple
  • 本文由 发表于 2023年7月13日 16:58:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定