You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React报错:setCurrentId is not a function 问题排查求助

解决Uncaught TypeError: setCurrentId is not a function错误

错误信息

Uncaught TypeError: setCurrentId is not a function at onClick (Post.js:19:1)

问题原因

你的Posts.js组件未添加导出语句,导致App.js无法正确引入该组件,进而setCurrentId函数没有被正常传递到子组件Post中,最终触发点击事件时的类型错误。

解决方案

在Posts.js文件末尾添加组件导出语句:

export default Posts;

修正后的完整Posts.js代码

import React from 'react';
import { Grid, CircularProgress } from '@material-ui/core';
import { useSelector } from 'react-redux';

import Post from './Post/Post';
import useStyles from './styles';

const Posts = ({ setCurrentId }) => {
  const posts = useSelector((state) => state.posts);
  const classes = useStyles();

  return (
    !posts.length ? <CircularProgress /> : (
      <Grid className={classes.container} container alignItems="stretch" spacing={3}>
        {posts.map((post) => (
          <Grid key={post._id} item xs={12} sm={6} md={6}>
            <Post post={post} setCurrentId={setCurrentId} />
          </Grid>
        ))}
      </Grid>
    )
  );
};

// 新增导出语句
export default Posts;

内容的提问来源于stack exchange,提问作者Atharv Kulkarni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 19:56:13