我一整天都在搜索,但是找不到解决我问题的方法。
我是React Native的新手,所以我陷入了困境...

这是我的问题:

我获取用于获取数据的api,我正在使用axios进行操作,可以渲染我的组件!

然后,我不想显示阵列的所有项目,但是我想通过2个按钮根据不同的值显示一些项目:

<Button onPress={Get ALL ITEMS with this value}/>
<Button onPress={Get ALL ITEMS with ANOTHER VALUE} />


我的问题是我正在用新信息修改阵列“产品”的状态,但这不是我想要的。我一直希望我根据数组中的特定值显示信息,而仍然获得我的完整数组...

这是我正在做的一些代码:

onPressFirstButton() {
let newArray = [...this.state.products];

this.setState({
  products: newArray.filter(function(product){
    return product.value == 32;
  })
});
}
onPressSecondButton() {
let otherArray = [...this.state.products];

this.setState({
  products: otherArray.filter(function(product){
    return product.other_value == 389;
  })
});
}


我敢肯定这不是最好的方法,因为它不起作用。希望您能理解我要说的话。

谢谢

最佳答案

由于您似乎使用了products属性来填充模板,因此可以考虑为此使用一个单独的变量,这样在进行过滤时就不必弄乱状态。也许这会有所帮助:

displayProducts = []

onPressFirstButton () {
  this.displayProducts = this.state.products.filter(product => /* logic */)
}

关于arrays - 过滤React Native数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52132586/

10-13 07:46
查看更多