我有一个在创建和编辑模式下使用的子组件。它具有选择菜单,该菜单不显示任何值,直到用户在创建模式下从选择菜单中选择一个选项为止。
在编辑模式下,此选择菜单应显示先前选择的选项。
下面是我的代码,
const Child = (mode: mode, formikBag: formikBag) => {
const handleValue = (field) => {
if (mode === 'edit') {
const selectedId = formikBag.values[itemId];
const selectedItems = allItems.filter((item) => item.id ===
itemId;
return selectedItem[0].name;
}
return allItems.filter((option) => option.value === field.value);
//should be returned in create mode
return(
<Select
value={handleValue}
/>
);
}
上面的代码在编辑或创建模式下不显示任何值。不知道出了什么问题。我是新来的反应。
有人可以帮我吗 谢谢。
handleValue
是一个函数,您需要执行它才能获得传回的收益
<Select value={() => handleValue(yourFieldHere)} />
如果Select
是您编写的组件,则可以在内部调用该函数Select
//In Select.js
//{value} means deconstruct that specific props passed from the parent, which name is 'value'. In this case, you have passed the handleValue through value. Therefore, you can use that function in the Select component
const Select = ({value}) => {
const selectValue = value(yourFieldName)
console.log(selectValue)
return (
<div>
//some content
</div>
)
}
export default Select
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句