制作包含调度的导出功能
我想发挥我可以导出并在项目周围使用它的功能。 问题是我需要使用调度。
import React from 'react'
import { useDispatch } from "react-redux";
import { displayMessage } from '../App/store/mixed'
const dispatch = useDispatch()
export const test = () => {
dispatch(displayMessage({ show: true, text: `test` }));
setTimeout(() => {
dispatch(displayMessage({ show: false, text: '' }))
}, 2000);
}
上面的代码将使您了解我要完成的工作。
这样的事情甚至可行吗?
I want to make function that I can export it and use it around the project.
The problem is that i need to use dispatch.
import React from 'react'
import { useDispatch } from "react-redux";
import { displayMessage } from '../App/store/mixed'
const dispatch = useDispatch()
export const test = () => {
dispatch(displayMessage({ show: true, text: `test` }));
setTimeout(() => {
dispatch(displayMessage({ show: false, text: '' }))
}, 2000);
}
The code above will give you idea what i am trying to accomplish.
Is something like this even doable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以制作一个thunk:
将被调用,例如
dispatch(test())
在其他地方。或者,您还可以从设置文件中 export store do
store.dispatch(...)
- 但这较少干净。You could make a thunk:
which would be invoked like
dispatch(test())
elsewhere.Or you could also
import
yourstore
from your setup file and dostore.dispatch(...)
- but that's a little less clean.