Javascript 选择组件渲染
我在react中创建了一个自定义选择组件,它看起来如下所示:Javascript 选择组件渲染,javascript,reactjs,Javascript,Reactjs,我在react中创建了一个自定义选择组件,它看起来如下所示: import { useDispatch, useSelector } from "react-redux"; const Select = ({ id, options, dispatchKey, selector, disabledOption = false, }) => { const dispatch = useDispatch(); const formValue =
import { useDispatch, useSelector } from "react-redux";
const Select = ({
id,
options,
dispatchKey,
selector,
disabledOption = false,
}) => {
const dispatch = useDispatch();
const formValue = useSelector((state) => state.form[selector]);
return (
<select
id={id}
required
onChange={(e) => dispatch({ type: dispatchKey, value: e.target.value })}
value={'IT'}
className="mt-1 block form-select w-full py-2 px-3 py-0 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:shadow-outline-blue focus:border-blue-300 transition duration-150 ease-in-out sm:text-sm sm:leading-5"
>
{disabledOption && (
<option value="" disabled>
{disabledOption}
</option>
)}
{options &&
options.map((o) => (
<option value={o.value} key={o.value}>
{o.text}
</option>
))}
</select>
);
};
export default Select;
const countries = [
{ value: "NL", text: "Nederland Add selected
props to the option that match the value
<option
selected={val === opt.value}
value={opt.value}
>
{opt.label}
</option>
从“react-redux”导入{useDispatch,useSelector};
常量选择=({
身份证件
选项,
调度键,
选择器,
disabledOption=false,
}) => {
const dispatch=usedpatch();
const formValue=useSelector((state)=>state.form[selector]);
返回(
分派({type:dispatchKey,value:e.target.value})}
值={'IT'}
className=“mt-1块形式选择w-full py-2 px-3 py-0边框边框-灰色-300 bg白色圆形md阴影sm焦点:轮廓无焦点:阴影轮廓蓝色焦点:边框-蓝色-300过渡持续时间-150缓进-输出sm:文本sm sm:前导-5”
>
{disabledOption&&(
{disabledOption}
)}
{选项&&
选项。映射((o)=>(
{o.text}
))}
);
};
导出默认选择;
我是这样使用它的:
import { useDispatch, useSelector } from "react-redux";
const Select = ({
id,
options,
dispatchKey,
selector,
disabledOption = false,
}) => {
const dispatch = useDispatch();
const formValue = useSelector((state) => state.form[selector]);
return (
<select
id={id}
required
onChange={(e) => dispatch({ type: dispatchKey, value: e.target.value })}
value={'IT'}
className="mt-1 block form-select w-full py-2 px-3 py-0 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:shadow-outline-blue focus:border-blue-300 transition duration-150 ease-in-out sm:text-sm sm:leading-5"
>
{disabledOption && (
<option value="" disabled>
{disabledOption}
</option>
)}
{options &&
options.map((o) => (
<option value={o.value} key={o.value}>
{o.text}
</option>
))}
</select>
);
};
export default Select;
const countries = [
{ value: "NL", text: "Nederland Add selected
props to the option that match the value
<option
selected={val === opt.value}
value={opt.value}
>
{opt.label}
</option>
const国家=[
{值:“NL”,文本:“荷兰将选定的道具添加到与值匹配的选项中
{disabledOption && (
<option value="" disabled>
{disabledOption}
</option>
)}
{opt.label}
将所选的道具添加到与该值匹配的选项中
{disabledOption && (
<option value="" disabled>
{disabledOption}
</option>
)}
{opt.label}
我想我可以解释Select
组件的初始负载行为
当您将disabledOption
作为道具传递时,false
的默认值现在设置为“Kies een land”
因此,当执行以下操作时:-
<option value="" disabled>
{disabledOption}
</option>
由于已禁用
,因此将显示来自国家/地区
的第一个条目,即NL
,我想我可以解释选择
组件的初始负载行为
当您将disabledOption
作为道具传递时,false
的默认值现在设置为“Kies een land”
因此,当执行以下操作时:-
<option value="" disabled>
{disabledOption}
</option>
由于禁用了
,将显示来自国家/地区的第一个条目,即NL
您的代码似乎没有问题(测试了有问题的确切代码)。只需检查是否有任何输入错误/尝试清除浏览器缓存/重新启动节点服务器。您的代码似乎没有问题(测试了有问题的确切代码)。只需检查是否有任何输入错误/尝试清除浏览器缓存/重新启动节点服务器。您将值硬编码为“IT”,您的选择值应该是一个变量(formValue?),默认设置为“IT”,是的,我知道,但“IT”用于测试目的,最终它将是动态的。但奇怪的是,“IT”没有显示在页面加载上,而是显示在“NL”上。我明白你的意思。我的错。你将值硬编码为“IT”,你的选择值应该是一个变量(formValue?),默认设置为“IT”是的,我知道,但“IT”用于测试目的,最终它将是动态的。但奇怪的是,“IT”没有显示在页面加载上,而是显示在“NL”上。我知道你在说什么。我的错。使用selected={true}对使用
标记中的值作出反应
标记中的
。在
标记中使用值
属性
标记,然后在
标记中使用选定项{true}
。