英文:
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} />
您可以在官方示例中找到更多信息 home#getting-started
英文:
The defaultValue
should be the Option
type, see StateManager Props
const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' },
];
// it should be
<SelectItemComp b={options[0]} options={options} />
// not
<SelectItemComp b='chocolate' options={options} />
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 "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;
答案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 "./SelectItemComp";
import "./styles.css";
export default function App() {
const options = [
{ id: 0, value: "default", label: "Select an option" },
{ id: 1, value: "chocolate", label: "Chocolate" },
{ id: 2, value: "strawberry", label: "Strawberry" },
{ id: 3, value: "vanilla", label: "Vanilla" }
];
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("default", selectedItem);
return (
<Select
defaultValue={options[0]}
onChange={handleChange}
options={options}
/>
);
};
// This might help you if you don't pass any options as props to SelectItemComp
// Happy coding
SelectItemComp.defaultProps = {
options: [{ id: 0, value: "default", label: "Select an option" }]
};
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 .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论