【发布时间】:2021-09-19 04:10:46
【问题描述】:
我有一个对象,里面有一个数组。每个数组有多个对象。所以使用 .map 函数,我在对象内的数组上循环。每个数组项都有一个单击功能,我可以在其中切换项。数组项的初始状态是this
{
DisplayText: "Foresatte"
Selectable: true
UserRecipientToInclude: "IncludeGuardianRecipient"
}
当我这样做时
choice.Selected = 假
并且控制台记录新添加的项目在对象中不存在的结果。这是为什么呢?
这里是代码
{c.UserDropdownMenuChoices.map(choice => {
return ( <TouchableHighlight
{...testProperties(choice.DisplayText, true)}
style={{ opacity: !choice.Selectable ? 0.4 : 1 }}
onPress={() => {
this.selectChoice(c.UserDropdownMenuChoices, choice, c)
}}
underlayColor={'transparent'}
key={choice.DisplayText}
>
<View style={styles.choiceContainer}>
{
choice.selected ? (
//<MaterialCommunityIcons name={'checkbox-marked'} style={styles.checkboxClick} size={20} />
<Icon
type={'material-community'}
name={'checkbox-marked'}
iconStyle={styles.checkboxClick}
size={20}
/>
) : (
<Icon
type={'material-community'}
name={'checkbox-blank-outline'}
iconStyle={styles.checkboxDefault}
size={20}
/>
)
//DONE: <MaterialCommunityIcons name={'checkbox-blank-outline'} style={styles.checkboxDefault} size={20} />
}
<Text style={styles.displayText}>{choice.DisplayText}</Text>
</View>
</TouchableHighlight> )}
而我的功能是这样的
selectChoice(choicesList, choice, contact) {
choice.selected = true
...
console.log(choice) // doesn't have the new property
}
此代码用于反应原生应用程序
【问题讨论】:
-
不清楚您的示例中的
choice是什么。请将其编辑为Minimal, Complete, and Verifiable example。
标签: javascript reactjs typescript react-native