Javascript React:TypeError:Object(…)不是函数。(onClick)

Javascript React:TypeError:Object(…)不是函数。(onClick),javascript,reactjs,Javascript,Reactjs,错误: TypeError:对象(…)不是函数 onClick src/SidebarChannel.js:12 return ( <div className="sidebarChannel" onClick={() => dispatch( setChannelInfo({ channelId: id, 返回( 派遣( setChannelInfo({ channel

错误: TypeError:对象(…)不是函数 onClick src/SidebarChannel.js:12

return (
    <div
      className="sidebarChannel"
      onClick={() =>
        dispatch(
          setChannelInfo({
            channelId: id,
返回(
派遣(
setChannelInfo({
channelId:id,
代码:

从“React”导入React;
从“react redux”导入{useDispatch};
从“/features/appSlice”导入{setChannelInfo};
导入“/SidebarChannel.css”;
函数SidebarChannel({id,channelName}){
const dispatch=usedpatch();
返回(
派遣(
setChannelInfo({
channelId:id,
channelName:channelName,
})
)
}
>
#
{channelName}
);
}
导出默认侧边栏通道;

第12行的onClick出现了错误,但我不知道如何修复它,或者它到底出了什么问题。

您可以尝试像这样为dispath添加brats包装器:``onClick={()=>{dispatch(setChannelInfo({channelId:id,channelName:channelName,})}```你的代码看起来完全正确,我很确定问题来自于你的setChannelInfo函数的导入。仔细检查,你会发现错误。你能在
setChannelInfo
中发布你应该导入的内容吗?我们可以调试,同意,我认为这个代码片段没有问题。请共享
appSlice
文件因此,我们可以看到您是如何导出您的操作的。
import React from "react";
import { useDispatch } from "react-redux";
import { setChannelInfo } from "./features/appSlice";
import "./SidebarChannel.css";

function SidebarChannel({ id, channelName }) {
  const dispatch = useDispatch();

  return (
    <div
      className="sidebarChannel"
      onClick={() =>
        dispatch(
          setChannelInfo({
            channelId: id,
            channelName: channelName,
          })
        )
      }
    >
      <h4>
        <span className="sidebarChannel__hash">#</span>
        {channelName}
      </h4>
    </div>
  );
}

export default SidebarChannel;