React寻找功能代码改进
我使用redux。每次我必须使用代码的一部分时:
const dispatch = useDispatch()
然后将功能称为:
dispatch(endpointError(true))
我想实现的目标是拥有一个地方,然后导出加上适当的函数。只需将其保存在一个地方:
const errorFunctions = () => {
const dispatch = useDispatch()
setEndpointError = () => dispatch(endpointError(true))
}
export const { setEndpointError } = errorFunctions
然后仅使用setendPoInterror()
我需要它即可。我认为它看起来很性感。 例如,当您需要将所有代码保持较短时,我认为是错误的,就像错误处理一样。
但是它不是合法的,我们不幸的是做到这一点。开发人员,您看到有我们可以编写并使其正常工作吗?
我的想法就像您有20个不同的屏幕一样,到处都是类似的错误处理,并且不编写每个位置const dispatch = usedispatch()
寻找改进:)
I use Redux. Every time I have to use part of the code:
const dispatch = useDispatch()
And then call the function like:
dispatch(endpointError(true))
What I want to achieve is to have some one place and then export plus call the proper function. Just keep it in one place like:
const errorFunctions = () => {
const dispatch = useDispatch()
setEndpointError = () => dispatch(endpointError(true))
}
export const { setEndpointError } = errorFunctions
And then just use only setEndpointError()
when I will need it. It looks sexy I think.
For example when you would need to keep your all code shorter it is a plus like in error handling I think so.
But there it is not legal and we cannot unfortunately make it. Devs do you see have can we write it and make it working?
My idea is like you have 20 different screens, everywhere would be similar error handling and would be good to not write every place const dispatch = useDispatch()
Looking for improvements :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为此创建一个自定义钩子:
比这样使用:
You can create a custom hook for this:
Than you use it like this: