【发布时间】:2020-05-22 19:40:44
【问题描述】:
从数组中删除项目仅对最后呈现的项目起作用,但从列表中删除选定的项目
const [otherPhones, setOtherPhones] = useState([]);
{otherPhones.map((item, i) => {
return (
<View
style={{
...DefaultStyles.iconContainer,
marginBottom: hp("1.25%")
}}
key={i}
>
<PhoneNumberPicker
placeholder={i18n.t("other_phone")}
style={{ flex: 10 }}
countryHint={countryHint}
onChange={value => {
_handleOtherPhone(value, i);
}}
/>
<IconButton
style={{ flex: 1 }}
icon="trash-can-outline"
color={SECONDARY}
size={SCALE_20}
onPress={_deleteOtherPhone.bind(this, i)}
/>
</View>
);
})}
const _deleteOtherPhone = index => {
const temp = [...otherPhones];
temp.splice(index, 1);
setOtherPhones(temp);
};
当我删除 PhoneNumberPicker 并仅显示简单的属性项时,一切正常。 PhoneNumberPicker 它是一个带有一些附加组件的 TextInput。
【问题讨论】:
-
用过滤器代替拼接怎么样?你试过了吗?
-
是的,我也尝试过,在这两种情况下,元素都从数组中删除,但子组件未正确更新。顺便感谢您的回复。
标签: javascript react-native react-hooks rendering