searchRecomon (item) {
this.setState({
searchVal: item,
searchCategoryId: 0,
sortType: 1,
priceSortStatus: 0
})
console.log(item)
console.log(this.state.searchVal)
}
/*查询商品*/
async searchGoods () {
const {searchVal, sortType, page, size, priceSortStatus, searchCategoryId} = this.state
const {data, filterCategory} = await http.getGoodsData({
keyword: searchVal,
page,
size,
sort: sortType === 1 ? 'id': 'price',
order: priceSortStatus > 0 ? 'asc': priceSortStatus === 0 ? 'default' : 'desc',
categoryId: searchCategoryId
})
this.setState({goodsList: data,searchKeyWordList: [], searchCategoryList: filterCategory})
this.getInitData()
在searchGoods方法里面并没有直接获取到最新的searchVal的值。解决方法:
searchRecomon (item) {
this.setState({
searchVal: item,
searchCategoryId: 0,
sortType: 1,
priceSortStatus: 0
}, () => {
this.searchGoods()
})
console.log(item)
console.log(this.state.searchVal)
}
在setState值后,里面传个回调函数作为参数
2、理解setState