reactjs - 将对象道具传递给 onSelect 回调未按预期工作
问题描述
我正在使用 react-bootstrap 下拉菜单。As per the documentation , the eventKey
prop will be passed to the onSelect
handler when a dropdown item is selected.
我正在尝试设置eventKey
一个对象,并将其传递给我的handleSelect(e)
处理程序,但是当我console.log()
将值传递给我的处理程序时,它给了我一个 string [object Object]
。
我的组件如下所示:
const CustomDropdown = props => {
function handleSelect(e) {
console.log(e);
//I expect this to print { name: 'genre', label: 'Genres' } but
//it prints '[object Object]'
}
return (
<Dropdown>
<Dropdown.Toggle as={React.Button} variant="link" id="dropdown-basic">
{props.text}
</Dropdown.Toggle>
<Dropdown.Menu>
{props.choices.map((choice, i) => ( //choices is an array of objects
<Dropdown.Item
key={i}
eventKey={choice} //The object I'm passing in. See below
onSelect={handleSelect}>
{choice.label}
</Dropdown.Item>
))}
</Dropdown.Menu>
</Dropdown>
);
};
该choice
对象如下所示:
{
name: 'genre',
label: 'Genres',
}
onSelect={handleSelect}
但是,如果我用箭头函数替换我的道具onSelect={() => handleSelect(choice)}
,对象就会正确地传递给 handleSelect,就像我想要的那样。为什么箭头函数方法传递对象,而原始方法没有?
解决方案
推荐阅读
- reactjs - 带有反应项目的 Json ejsonparse 消息必须是实际的 json 而不是 JavaScript
- apache-spark - 如何使用 Scala 在 Jupyter 笔记本内的 UDF 中使用广播
- azure - 为什么“Microsoft.Web/sites”的“FileSystemUsage”指标总是返回零?
- javascript - React Native API 数据获取并保存为全局数据
- java - Kafka连接音频文件
- moodle - 具有固定和预先指定数量的正确和错误答案的问题
- php - Laravel - 从产品表中获取结果,我将结果与属性表匹配
- xamarin - 更新 Nuget 包后缺少 Xamarin 代码
- c# - 程序集 '' 引用程序集 'microsoft.sqlserver.dtsruntimewrap, ',它在当前数据库中不存在
- java - 如何使用流和 lambda 从 int[] 数组中打印和选择数字