【发布时间】:2021-07-09 00:29:29
【问题描述】:
我正在努力从 useEffect 传递参数。 在下图的POST方法中,生成了一个ID(res.data._id),但是不知道怎么传给后面用到的下一个方法
useEffect(() => {
axios
.post('/api/hist', {
user,
collId: `${id}`,
answers
})
.then((res) => {
console.log('id:', res.data._id)
});
}, []);
每次单击按钮时都会使用另一种方法
const saveAnswer = (answer: string, isCorrect?: boolean) => async () => {
axios
.post('/api/answer', {
isCorrect: isCorrect,
flashcardId: answer
})
.then((res) => {
console.log(res.data)
});
};
但是在这里我想使用之前生成的这个 ID 作为 histID :
.post('/api/answer', {
isCorrect: isCorrect,
flashcardId: answer,
histID: ???
})
有人知道如何实现吗?
【问题讨论】:
-
id变量在哪里声明并从哪里生成值?不能在其他功能中再次使用吗?
标签: node.js reactjs axios react-hooks