从组件中获取到数据之后,要把id数据赋值给接口中的id,结果接口数据undefined?或者直接没赋值上,一直保持初始值不变。
部分代码如下:
const [Id, setId] = useState(34);
function showmodal(data1) {
setId(data1.id)
// setId(parseInt(data1.id))
console.log('获取导航栏id', data1)
console.log('Id', Id)
console.log('data1.Id', data1.id)
getArticleList()
}
const getArticleList = useCallback((passPage) => {
return request({
url: '/getArticleList',
method: 'get',
data: {
type: 1,
catalog_id: Id
},
// success: function () {
// articleDetails()
// }
})
}, [page])
{
(list && list.length > 0) ?
<ScrollView
className={styles.top}
// scrollY
// refresherEnabled
// refresherTriggered={refreshTriggered}
lowerThreshold={60}
bindscrolltolower={loadMore}
bindrefresherrefresh={() => {
// setRefreshTriggered(true)
init();
}}>
{
list.map((v, i) => {
return (
<HelptitleItem key={i} {...v} showModal={() => { showmodal(v) }} id="helptitleItem" />
)
})
}
</ScrollView>
: null
}
运行截图:
试试showModal这样写
const showModal= (data1) => { new Promise((resolve) => { setId((data1.id) => { resolve(data1.id); return data1.id; }); }).then(res => { getArticleList() }); };
setId(data1.id)异步问题