自定义React挂钩抛出错误“ React”已检测到SupplyPlaidLink调用的挂钩顺序的变化。”
我正在尝试为格子API的链接功能制作自定义式钩子。我的自定义挂钩代码看起来像这样:
function useProvidePlaidLink() {
const auth = useAuth();
// @ts-ignore
if (!auth.user) return null;
const [linkToken, setLinkToken] = useState(null);
const fetchToken = useCallback(async () => {
try {
const config = {
method: "post",
headers: {
'earmark-api-key': process.env.EARMARK_API_KEY,
},
params: {
// @ts-ignore
user_id: auth.user.uid
},
url:'/api/createLinkToken',
}
const response = await axios(config);
setLinkToken(response.data.linkToken);
} catch (error) {
}
}, []);
useEffect(() => {
fetchToken();
}, [fetchToken]);
const onSuccess = useCallback(async (publicToken, metadata) => {
const config = {
method: "post",
headers: {
'earmark-api-key': process.env.EARMARK_API_KEY,
},
params: {
// @ts-ignore
user_id: auth.user.uid,
publicToken: publicToken,
},
url: '/api/exchangeLinkToken',
};
try {
const response = await axios(config);
} catch (error) {
}
}, []);
const config = {
token: linkToken,
onSuccess,
}
const { open, exit, ready } = usePlaidLink(config);
return { open, exit, ready, fetchToken }
}
前3行正在调用我的自定义验证挂钩以获取用户用户ID。当页面首次渲染时,这是不确定的,但是稍后拆分后,它会加载在UID中。但是,在不确定的情况下,我的格子链接代码会丢弃错误。因此,我在格子链接代码中添加了一个快速IF语句以检查是否已定义,如果不返回,则不返回并且不要执行代码。然后,这引发了错误React已检测到SupplyPlaidLink调用的挂钩顺序的变化。如果不解决,这将导致错误和错误。有关更多信息,请阅读挂钩规则:
我不认为这是一件大事,因为如果我只等待拆分的第二个)然后,我可以运行该功能。但是我不喜欢看到这个错误,所以我应该如何解决这个错误? 谢谢
I'm trying to make a custom react hook for the plaid api's Link feature. My code for the custom hook looks like this:
function useProvidePlaidLink() {
const auth = useAuth();
// @ts-ignore
if (!auth.user) return null;
const [linkToken, setLinkToken] = useState(null);
const fetchToken = useCallback(async () => {
try {
const config = {
method: "post",
headers: {
'earmark-api-key': process.env.EARMARK_API_KEY,
},
params: {
// @ts-ignore
user_id: auth.user.uid
},
url:'/api/createLinkToken',
}
const response = await axios(config);
setLinkToken(response.data.linkToken);
} catch (error) {
}
}, []);
useEffect(() => {
fetchToken();
}, [fetchToken]);
const onSuccess = useCallback(async (publicToken, metadata) => {
const config = {
method: "post",
headers: {
'earmark-api-key': process.env.EARMARK_API_KEY,
},
params: {
// @ts-ignore
user_id: auth.user.uid,
publicToken: publicToken,
},
url: '/api/exchangeLinkToken',
};
try {
const response = await axios(config);
} catch (error) {
}
}, []);
const config = {
token: linkToken,
onSuccess,
}
const { open, exit, ready } = usePlaidLink(config);
return { open, exit, ready, fetchToken }
}
The first 3 lines are calling my custom auth hook to get the users user id. When the page first renders this is undefined, but a split second later it loads in the uid. However for that split second when it's undefined, my plaid link code throws errors. So I added a quick if statement to the plaid link code to check if it's defined, and if not then return nothing and don't execute the code. This then throws the error React has detected a change in the order of Hooks called by ProvidePlaidLink. This will lead to bugs and errors if not fixed. For more information, read the Rules of Hooks:
I don't think it's that big of a deal because if I just wait for the split second (and no one will be clicking on the link bank button instantly on page load) then I am able to run the function no problem. However I don't like seeing that error so how should I go about fixing this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在开始时,您检查
auth.user
isnull
,如果是,则返回该功能。这将导致每个挂钩 null检查将不会运行,这将导致错误。要解决这个问题,即使用户为null,您也必须运行挂钩。
该代码应该解决问题:
In the beginning, you check if
auth.user
isnull
, and if it is, you return out of the function. This will cause that every hook after the null check will not be run, and that will cause the error.To solve this, you have to run the hooks even if user is null.
This code should solve the problem: