Javascript 我能';t在状态中设置空数组
我正在设置一个应用程序,在这个应用程序中有一个列表,列表中的项目有一个标题和一个删除项目的按钮,我可以删除几乎所有的项目,但当剩余的一个元素我不能删除,但我可以。。。我只是不能设置一个空数组。我用lodash去除元素 我尝试将新数组放入aux var中,检查大小是否等于零,并直接设置空数组,而不是使用remove函数来执行此操作Javascript 我能';t在状态中设置空数组,javascript,reactjs,react-native,lodash,Javascript,Reactjs,React Native,Lodash,我正在设置一个应用程序,在这个应用程序中有一个列表,列表中的项目有一个标题和一个删除项目的按钮,我可以删除几乎所有的项目,但当剩余的一个元素我不能删除,但我可以。。。我只是不能设置一个空数组。我用lodash去除元素 我尝试将新数组放入aux var中,检查大小是否等于零,并直接设置空数组,而不是使用remove函数来执行此操作 class FuelReservesFilterView extends React.Component { state = { plates: [] };
class FuelReservesFilterView extends React.Component {
state = { plates: [] };
goToOperationsSearch = () => {
Actions.selectOperationView({
backSceneKey: "fuelReservesFilterView",
});
};
goToPlateSearch = () => {
Actions.selectPlateView({
backSceneKey: "fuelReservesFilterView",
});
};
goToStatusSearch = () => {
Actions.selectStatusVehicleView({
backSceneKey: "fuelReservesFilterView",
});
};
componentDidUpdate() {
if (this.props.placa) {
if (_.last(this.state.plates) != this.props.placa) {
this.setState({
plates: [...this.state.plates, this.props.placa],
});
}
}
}
renderBtnOperacao() {
if (!this.props.operacao)
return (
<ButtonRounded
onPress={this.goToOperationsSearch}
textColor={ACCENT_COLOR}
label={i18n.t("Select_operation")}
/>
);
else
return (
<TouchableOpacity onPress={this.goToOperationsSearch}>
<View>
<TextTitle style={styles.textStyle}>
{this.props.operacao.nome}
</TextTitle>
</View>
</TouchableOpacity>
);
}
renderBtnStatus() {
if (!this.props.status)
return (
<ButtonRounded
onPress={this.goToStatusSearch}
textColor={ACCENT_COLOR}
label={i18n.t("Select_status_vehicle")}
/>
);
else
return (
<TouchableOpacity onPress={this.goToStatusSearch}>
<View>
<TextTitle style={styles.textStyle}>
{this.props.status.nome}
</TextTitle>
</View>
</TouchableOpacity>
);
}
renderPlateItem({ item }) {
return (
<ClearItem
withIcon
onPressIcon={() => {
this.removerItem(item);
}}
id={item.id}
text={item.placa}
icon="close"
/>
);
}
removerItem = item => {
this.setState({ plates: _.remove(this.state.plates, function(plate) {
return plate.placa != item.placa;
})
});
};
renderPlatesList() {
if (_.size(this.state.plates) > 0) {
return (
<FlatList
style={this.props.style}
data={this.state.plates}
renderItem={this.renderPlateItem.bind(this)}
keyExtractor={plate => plate.placa}
/>
);
}
}
render() {
return (
<ScrollView style={styles.container}>
<ButtonRounded
onPress={this.goToPlateSearch}
textColor={ACCENT_COLOR}
label={i18n.t("Select_plate")}
/>
{this.renderPlatesList()}
<Divider space={20} />
{this.renderBtnOperacao()}
<Divider space={20} />
{this.renderBtnStatus()}
<Divider space={20} />
<TextTitle>{i18n.t("Minimal_level")}</TextTitle>
<CustomSlider
min={0}
max={100}
metric="%"
onValueChange={value => console.log(value)}
/>
<TextTitle>{i18n.t("Maximum_level")}</TextTitle>
<CustomSlider
min={0}
max={100}
value={100}
metric="%"
onValueChange={value => console.log(value)}
/>
<Divider space={20} />
<ButtonRounded
style={{ marginBottom: 50 }}
textColor={PRIMARY_COLOR}
label={i18n.t("Apply_filter")}
/>
</ScrollView>
);
}
}
const styles = StyleSheet.create({
container: {
padding: PADDING_VIEW,
},
textStyle: {
textAlign: "center",
},
});
const mapStateToProps = state => {
return {};
};
export default connect(
mapStateToProps,
{},
)(FuelReservesFilterView);
类FuelReservesFilterView扩展了React.Component{
状态={plates:[]};
goToOperationsSearch=()=>{
操作。选择操作视图({
backSceneKey:“fuelReservesFilterView”,
});
};
goToPlateSearch=()=>{
Actions.selectPlateView({
backSceneKey:“fuelReservesFilterView”,
});
};
goToStatusSearch=()=>{
Actions.selectStatusVehicleView({
backSceneKey:“fuelReservesFilterView”,
});
};
componentDidUpdate(){
如果(这个。道具。普拉卡){
如果(u.last(this.state.plates)!=this.props.placa){
这是我的国家({
盘子:[…this.state.plates,this.props.placa],
});
}
}
}
renderBtnOperacao(){
如果(!this.props.operacao)
返回(
);
其他的
返回(
{this.props.operacao.nome}
);
}
renderBtnStatus(){
如果(!this.props.status)
返回(
);
其他的
返回(
{this.props.status.nome}
);
}
renderPlateItem({item}){
返回(
{
本项目(项);
}}
id={item.id}
text={item.placa}
icon=“关闭”
/>
);
}
RemoveItem=项目=>{
this.setState({plates:u.remove(this.state.plates,函数(plate)){
返回板.placa!=项目.placa;
})
});
};
renderPlatesList(){
如果(u.尺寸(本州板材)>0){
返回(
placa}板
/>
);
}
}
render(){
返回(
{this.renderPlatesList()}
{this.renderBtnOperacao()}
{this.renderBtnStatus()}
{i18n.t(“最小水平”)}
console.log(值)}
/>
{i18n.t(“最高水平”)}
console.log(值)}
/>
);
}
}
const styles=StyleSheet.create({
容器:{
填充:填充视图,
},
文本样式:{
textAlign:“居中”,
},
});
常量mapStateToProps=状态=>{
返回{};
};
导出默认连接(
MapStateTops,
{},
)(燃料储备过滤视图);
为什么要使用lodash?它可以通过内置阵列的函数来完成。Remove lodash函数正在对React中不可接受的状态变量进行变异
state = {
items: [
{ name: "John", id: 1 },
{ name: "Paul", id: 2 },
{ name: "jonathan", id: 3 }
]
};
removeItem = id =>
this.setState(prev => ({
items: prev.items.filter(item => id !== item.id)
}));
render() {
return (
<div className="App">
{this.state.items.map(item => (
<div onClick={() => this.removeItem(item.id)} key={item.id}>
remove {item.name} id {item.id}
</div>
))}
</div>
);
}
状态={
项目:[
{姓名:“约翰”,身份证号码:1},
{姓名:“保罗”,身份证号码:2},
{姓名:“乔纳森”,身份证号码:3}
]
};
removietem=id=>
this.setState(prev=>({
items:prev.items.filter(item=>id!==item.id)
}));
render(){
返回(
{this.state.items.map(item=>(
this.removietem(item.id)}key={item.id}>
删除{item.name}id{item.id}
))}
);
}
我听不懂你说什么。你能说得更清楚一点吗?我有一个3个元素的数组,设置在flatlist中,等等。为了将数据设置到flatlist,我使用“状态”,所以当我逐个删除元素时,最后一个a我无法删除您在控制台中看到的任何错误?我认为您正在传递this.state.plates
根据lodash文档,这是一种常量类型尝试传递plates数组的本地副本以删除函数,然后检查大小并设置状态。我不明白,您能给我一个示例吗?我认为这不是问题,即使在我调用setState({plates:[]})时,上一个元素也发生了同样的事情。我认为您应该根据我的建议进行一些更改。您的组件diddupdate
可能是一个问题。我没有沙盒,但如果我正确地认为,这个.props.placa
是数组中的一个元素。当组件更新且处于状态的数组为空时(与元素比较undefined
),该条件始终为真,并且使用该元素更新新状态。如果道具中的元素与组件状态中的元素相同,我是对的,我认为你是对的,有意义。现在我有另一个问题--'什么样的问题?我必须清除道具以使if子句为false,所以我使用Action.refresh({placa:undefined})并解决了我的问题:),谢谢