简体   繁体   English

错误:元素类型无效:应为字符串或类/函数,但得到:未定义

[英]Error: Element type is invalid: expected a string or a class/function but got: undefined

Error image The error specifically points to:错误图片错误具体指向:

dispatch({
   type: SET_POSTS,
   payload: res.data
})

Below is my func code:下面是我的函数代码:

export const getPosts = () => dispatch => {
    dispatch({ type: LOADING_DATA })
    axios.get('/posts')
        .then(res => {
            dispatch({
                type: SET_POSTS,
                payload: res.data
            })
        })
        .catch(err => {
            dispatch({
                type: SET_POSTS,
                payload: []
            })
        })
};

I checked for imports, console.log(res) and everything seems fine, but the error always points to dispatch()我检查了导入,console.log(res),一切似乎都很好,但错误总是指向 dispatch()

Edit: After tracing the code with redux dev tools I found out that the payload for SET_POSTS kept changing from payload:[{...}] to undefined.编辑:使用 redux 开发工具跟踪代码后,我发现SET_POSTS的有效负载不断从payload:[{...}]变为未定义。 Below I attached the code for my reducer.下面我附上了我的减速器的代码。 I still don't know what the problem is.我仍然不知道问题是什么。

import { SET_POSTS, LIKE_POST, UNLIKE_POST, LOADING_DATA } from '../types';

const initialState = {
    posts: [],
    post: {},
    loading: false,
};

export default function (state = initialState, action) {
    switch (action.type) {
        case LOADING_DATA:
            return {
                ...state,
                loading: true
            };
        case SET_POSTS:
            return {
                ...state,
                posts: action.payload,
                loading: false,
            };
        case LIKE_POST:
        case UNLIKE_POST:
            var index = state.posts.findIndex((post) => post.postId === action.payload.postId);
            state.posts[index] = action.payload;
            // conditional from github
            if (state.post.postId === action.payload.postId) {
                state.post = action.payload;
            }
            return {
                ...state
            };
        default:
            return state;
    }
}

Here is where the getPosts() function is been rendered:这里是 getPosts() function 被渲染的地方:

import { getPosts } from '../redux/actions/dataActions';

class home extends Component {

    componentDidMount() {
        this.props.getPosts();
    }
    render() {
        const { posts, loading } = this.props.data;
        let recentPostsMarkup = !loading ? (
            posts.map(post => <Post key={post.postId} post={post} />)
        ) : (<p>loading...</p>);
return (
            <Grid container spacing={5}>
                <Grid item sm={8} xs={12}>
                    {recentPostsMarkup}
                </Grid>
                <Grid item sm={4} xs={12}>
                    <Profile />
                </Grid>
            </Grid>
        );

home.propTypes = {
    getPosts: PropTypes.func.isRequired,
    data: PropTypes.object.isRequired,
}

const mapStateToProps = state => ({
    data: state.data
});
export default connect(mapStateToProps, { getPosts })(home);

This is the Post.js file where the post prop is being passed in the home.js file.这是在Post.js文件中传递post属性的home.js文件。

class Post extends Component {
    likedPost = () => {
        if (this.props.user.likes && this.props.user.likes.find(
            like => like.postId === this.props.post.postId)
        ) return true;
        else return false;
    };
    likePost = () => {
        this.props.likePost(this.props.post.postId);
    }
    unlikePost = () => {
        this.props.unlikePost(this.props.post.postId);
    }
    render() {
        dayjs.extend(relativeTime);
        const {
            classes,
            post: { body, createdAt, userImage, userHandle, postId, likeCount, commentCount },
            user: {
                authenticated
            }
        } = this.props;
        const likeButton = !authenticated ? (
            <MyButton tip="Like">
                <Link to="/login">
                    <FavoriteBorder color="primary"/>
                </Link>
            </MyButton>
        ) : (
            this.likedPost() ? (
                <MyButton tip="Undo like" onClick={this.unlikePost}>
                    <FavoriteIcon color="primary"/>
                </MyButton>
            ): (
                <MyButton tip="Like" onClick={this.likePost}>
                    <FavoriteBorder color="primary"/>
                </MyButton>
            )
        )
        return (
            <Card className={classes.card}>
                <CardMedia
                    image={userImage}
                    title="Profile Image" className={classes.image} />
                <CardContent className={classes.content}>
                    <Typography
                        variant="h5"
                        component={Link}
                        to={`/users/${userHandle}`}
                        color="primary">
                        {userHandle}
                    </Typography>
                    <Typography variant="body2" color="textSecondary">
                        {dayjs(createdAt).fromNow()}
                    </Typography>
                    <Typography variant="body1">
                        {body}
                    </Typography>
                    {likeButton}
                    <span>{likeCount} Likes</span>
                    <MyButton tip="comments">
                        <ChatIcon color="primary" />
                    </MyButton>
                    <span>{commentCount} comments</span>
                </CardContent>
            </Card>
        )
    }
};


Ciao, this is a well known issue in React. Ciao,这是 React 中众所周知的问题。 Please, check this guide (even if is made for React Native).请检查本指南(即使是为 React Native 制作的)。 In brief, your problem is related on how you are importing your component, and is not related on how you are dispatching your data.简而言之,您的问题与您如何导入组件有关,与您如何分派数据无关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 元素类型无效:应为字符串或类/函数,但得到:未定义 - Element type is invalid: expected a string or a class/function but got: undefined 错误:元素类型无效:应为字符串或类/函数,但得到:未定义。 导入错误 - Error: Element type is invalid: expected a string or a class/function but got: undefined. Import error 错误:缩小的 React 错误 #130:元素类型无效:预期为字符串或类/函数,但得到:未定义 - Error: Minified React error #130: Element type is invalid: expected a string or a class/function but got: undefined 错误 - 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - error - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 错误“元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义” - Error "Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined" 错误:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义 - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 渲染错误元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - Render Error Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 错误:元素类型无效:应为字符串或类/函数,但未定义。 反应本机 - Error: Element type is invalid: expected a string or a class/function but got undefined. React Native 错误:元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合函数)但得到:未定义 - Error: Element type is invalid: expected a string (for build-in components) or a class/ function( for composite functions) but got : undefined 控制台错误:元素类型无效:预期为字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义 - Console error: Element type is invalid:expected a string (for built-in components) or a class/function (for composite components) but got: undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM