初始化常量中的参数

huangapple go评论94阅读模式
英文:

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: &quot;bizz&quot;
};

foo = { // &lt;-- error, can&#39;t reassign foo
  bar: &quot;buzz&quot;
};

foo.bar = &quot;buzz&quot;; // &lt;-- 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: &quot;ADD_CLICKED&quot; });

Does work

const dispatch = useAppDispatch();

...

dispatch({ type: &quot;ADD_CLICKED&quot; });

While you could try something like useAppDispatch()({ type: &quot;ADD_CLICKED&quot; }); 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: &quot;ADD_CLICKED&quot;});

huangapple
  • 本文由 发表于 2023年8月8日 23:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定