简体   繁体   English

输入评论后出现关键错误。 我不知道如何解决(反应问题)

[英]I got a key error after entering a comment. I don't know how to fix (react propblem)

I got a key error after entering a comment.输入评论后出现关键错误。 But I can't find a reason.但我找不到原因。

Problem: Screen flashes after comment I get a key error and can't figure out where the key error is.问题:评论后屏幕闪烁 我收到一个关键错误,但无法确定关键错误在哪里。

Here's how I tried to fix the key error:以下是我尝试修复关键错误的方法:

key = {c} => key = {c.id}

What causes a key error?是什么导致密钥错误?

error message is this:错误信息是这样的:

VM152 _app.js:22355 Uncaught TypeError: Cannot read property 'key' of undefined
    at List._this.renderItem (VM152 _app.js:22355)
    at VM152 _app.js:22465
    at Array.map (<anonymous>)
    at List._this.renderList (VM152 _app.js:22464)
    ...

code is this代码是这个

const Home = () => {
    const dispatch = useDispatch();
    const { isLoggedIn, me } = useSelector(state => state.user);
    const { mainPosts } = useSelector(state => state.post);

    useEffect(() => {
        // alert("LOAD_MAIN_POSTS_REQUEST 실행")
        dispatch({
            type: LOAD_MAIN_POSTS_REQUEST,
        })
    }, []);

    return (
        <>
            {me && <PostForm />}
            {mainPosts.map((c) => {
                return (
                    <PostCard key={c.id} post={c} />
                );
            })}
        </>
    );
};

The first time the Home component renders the mainPosts array may not have any data, after useEffect is called and your action is dispatched the data will be present. Home 组件第一次渲染 mainPosts 数组时可能没有任何数据,在调用 useEffect 并调度您的操作后,数据将呈现。 To account for the data not being there on intial render you can validate mainPosts to check for null or undefined.要考虑初始渲染中不存在的数据,您可以验证 mainPosts 以检查 null 或未定义。 That way the.map will only take place when mainPosts is truthy.这样,.map 只会在 mainPosts 为真时发生。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM