英文:
Parameters in initialized const
问题
我在Redux中想要分发一个事件,但是我注意到在JavaScript中定义const
有点令人困惑。当我尝试直接向useAppDispatch
函数添加一个参数时,linter会标记一个错误。
但是当我将dispatch
函数放在一个const
中时,我可以添加一个参数:
有人可以解释一下这个问题吗?
英文:
I'm in a situation where I want to dispatch an event in Redux. I'm noticing that defining const
in Javascript is a little bit confusing. When I'm trying to add a parameter directly to the useAppDispatch
function, the linter marks an error.
import { PlusCircleIcon } from "@heroicons/react/20/solid";
import React from "react";
import { useAppDispatch } from "../../store/hooks";
const OptionsPanelAddAction = ():JSX.Element => {
const handleOnClick = (e: React.MouseEvent<HTMLButtonElement>):void => {
e.preventDefault();
useAppDispatch({type: "ADD_CLICKED"}); ---> Expected 0 arguments, but got 1
}
return (
<div className="bg-white m-4 p-2 drop-shadow-sm flex justify-center">
<div>
<a><PlusCircleIcon className="h-6 w-6" /></a>
</div>
</div>
)
}
export default OptionsPanelAddAction;
But when I put the dispatch
function in a const I'm able to add an parameter:
import { PlusCircleIcon } from "@heroicons/react/20/solid";
import React from "react";
import { useAppDispatch } from "../../store/hooks";
const OptionsPanelAddAction = ():JSX.Element => {
const dispatch = useAppDispatch();
const handleOnClick = (e: React.MouseEvent<HTMLButtonElement>):void => {
e.preventDefault();
dispatch({type: "ADD_CLICKED"}); ---> No Error!
}
return (
<div className="bg-white m-4 p-2 drop-shadow-sm flex justify-center">
<div>
<a><PlusCircleIcon className="h-6 w-6" /></a>
</div>
</div>
)
}
export default OptionsPanelAddAction;
Can somebody explain this to me?
答案1
得分: 3
关于const
声明
const dispatch = useDispatch()
并不是将useDispatch
函数的const
引用存储在dispatch
中,而是调用useDispatch
函数并将其返回值,即dispatch
函数的const
引用存储在其中。
在JavaScript(和TypeScript)中,声明一个变量为const
只意味着该变量不能被重新赋值,但它可以被改变。
const foo = {
bar: "bizz"
};
foo = { // <-- 错误,不能重新赋值给foo
bar: "buzz"
};
foo.bar = "buzz"; // <-- 正确,我们可以改变foo对象
在React中,我们将组件变量声明为const
,表示在React组件渲染周期内它们不会被重新赋值,即在组件渲染期间引用不会改变。
关于useAppDispatch
钩子
useAppDispatch
是一个React钩子,必须在返回dispatch
函数之前单独调用。useAppDispatch
函数不接受任何参数。操作对象被传递给dispatch
函数,而不是钩子。
不起作用
useAppDispatch({ type: "ADD_CLICKED" });
起作用
const dispatch = useAppDispatch();
...
dispatch({ type: "ADD_CLICKED" });
虽然你可以尝试像useAppDispatch()({ type: "ADD_CLICKED" })
这样的写法,但我不建议这样做,因为这不是常见的使用模式,可能会降低可读性/可维护性,甚至可能违反Hooks规则,具体取决于你尝试调用它的位置。
英文:
About const
declarations
const dispatch = useDispatch()
isn't storing a const
reference to the useDispatch
function, it's calling the useDispatch
function and storing a const
reference of its return value, the dispatch
function.
In Javascript (and Typescript) declaring a variable const
only means the variable can't be reassigned a value, though it can be mutated.
const foo = {
bar: "bizz"
};
foo = { // <-- error, can't reassign foo
bar: "buzz"
};
foo.bar = "buzz"; // <-- OK, we can mutate the foo object
In React we declare component variables const
to indicate they will not be reassigned a value for the duration of the React component render cycle, i.e. the reference won't change during the component render.
About the useAppDispatch
hook
useAppDispatch
is a React hook that must be called on its own first prior to returning the dispatch
function. The useAppDispatch
function does not consume any arguments. The action object is passed to the dispatch
function, not the hook.
Doesn't work
useAppDispatch({ type: "ADD_CLICKED" });
Does work
const dispatch = useAppDispatch();
...
dispatch({ type: "ADD_CLICKED" });
While you could try something like useAppDispatch()({ type: "ADD_CLICKED" });
I wouldn't recommend this at all as it is not a common use pattern and likely hurts readability/maintainability and may even break the Rules of Hooks given where you might be trying to call it.
答案2
得分: 0
useAppDispatch
是一个钩子函数,它返回Redux存储中的dispatch函数的引用。这是文档。
你需要调用这个钩子函数,然后可以像这样使用引用:
const dispatch = useAppDispatch()
dispatch({type: "ADD_CLICKED"});
英文:
useAppDispatch
is a hook which returns a reference to the dispatch function from the Redux store. here is the doc
you have to call the hook and then you can use the reference like this:
const dispatch = useAppDispatch()
dispatch({type: "ADD_CLICKED"});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论