可以从redux thunk中使用路由器。这是一个好习惯吗?
我有这个锚点元素:
<a
className="btn btn-sm btn-circle"
href={`https://www.facebook.com/sharer/sharer.php?u=${
process.env.NEXT_PUBLIC_ENVIRONMENT == "prod"
? "https://tikex.com"
: "https://tikex-staging.com"
}/share/${organizationSlug}/${postId}"e=${postSurveySchemaDTO?.caption}`}
onClick={(e) => {
dispatch(
createShare({
tempUserId: "e3445c3b-5513-4ede-8229-d258ed4418ae",
postId,
})
);
}}
>
Megosztom
</a>
我进行了多次测试,也许只有一次,但似乎比OnClick进行了,在这种情况下没有触发网络,99%可以。如果我也从onClick
触发导航,这会更好吗?
我可以从reducer
/thunk
触发router.push()
吗?
像这样?
export const createShare = createAsyncThunk(
`${namespace}/createShare`,
async (props: any, { dispatch }) => {
const { data } = await axios({
method: 'post',
url: 'share',
data: props,
headers: { crossDomain: true },
}).then((res) => {
router.push(`http://...`)
return res
})
return data
}
)
I have this anchor element:
<a
className="btn btn-sm btn-circle"
href={`https://www.facebook.com/sharer/sharer.php?u=${
process.env.NEXT_PUBLIC_ENVIRONMENT == "prod"
? "https://tikex.com"
: "https://tikex-staging.com"
}/share/${organizationSlug}/${postId}"e=${postSurveySchemaDTO?.caption}`}
onClick={(e) => {
dispatch(
createShare({
tempUserId: "e3445c3b-5513-4ede-8229-d258ed4418ae",
postId,
})
);
}}
>
Megosztom
</a>
I tested many times, maybe one time only but seem than onclick and networking was not triggered in that case, 99% ok. Is it better if I trigger navigation also from onClick
after networking was done?
Can I trigger router.push()
from reducer
/ thunk
?
Like this?
export const createShare = createAsyncThunk(
`${namespace}/createShare`,
async (props: any, { dispatch }) => {
const { data } = await axios({
method: 'post',
url: 'share',
data: props,
headers: { crossDomain: true },
}).then((res) => {
router.push(`http://...`)
return res
})
return data
}
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您有种族状况。
&lt; a&gt;
正在尝试将浏览器导航到href
onclick
onclick 具有dispatch(createShare(... )
触发器,我建议防止默认
&lt; a&gt;
事件,并手动编排两个单独的事件 -如果您需要
Router
进行导航,则可以通过派遣行动的thunk-It seems like you have a race condition.
<a>
is trying to navigate the browser away to thehref
onClick
has adispatch(createShare(...))
triggerI would suggest preventing the default
<a>
event and manually choreograph the two separate events -If you need
router
to navigate, you can pass a thunk to the dispatched action -