【发布时间】:2018-10-12 06:25:25
【问题描述】:
案例一:
我将指定选项值selectedZone 设置为我的<DropDownMenu /> 它工作正常。
<DropDownMenu /> 是我的选择器:
render() {
let { zone, selectedZone, selectedCity } = this.state;
return (
<DropDownMenu
style={{
selectedOption: {
marginBottom: -5
}
}}
styleName="horizontal"
options={zone}
selectedOption={selectedZone || zone[0]}
onOptionSelected={(zone) => {
this.setState({ selectedZone: zone, selectedCity: zone.children[0] });
}}
titleProperty="brand"
valueProperty="id"
/>
...
)
}
案例 2:
当我从我的 AsyncStorage 中指定值 selectedZone(值相同)时,它不起作用并显示黄色警告。
所以我尝试检查源代码。
函数getSelectedOption()来自<DropDownMenu />源代码。
getSelectedOption() {
const { options, selectedOption } = this.props;
console.log('check options');
console.log(options);
console.log('check selectedOption');
console.log(selectedOption);
console.log('check _.indexOf(options, selectedOption');
console.log(_.indexOf(options, selectedOption));
if (_.indexOf(options, selectedOption) === -1) {
console.warn(
`Invalid \`selectedOption\` ${JSON.stringify(selectedOption)}, ` +
'DropDownMenu `selectedOption` must be a member of `options`.' +
'Check that you are using the same reference in both `options` and `selectedOption`.'
);
return;
}
return selectedOption;
}
lodash 函数 indexOf 将在案例 1 中返回 1。
在案例 2 中重新运行 -1,我认为它应该像案例 1 一样返回 1
我与options 和selectedOption 进行比较,看不出案例 1 和案例 2 有什么不同。
有人可以教我我错过了哪一步?
任何帮助将不胜感激。提前致谢。
【问题讨论】:
标签: javascript react-native lodash