gpt4 book ai didi

Error: Type 'void' is not assignable to type 'ReactNode' array.map() [closed](错误:类型‘void’不可赋值给类型‘ReactNode’array.map()[Closed])

转载 作者:bug小助手 更新时间:2023-10-25 21:54:11 26 4
gpt4 key购买 nike




I'm getting an error while calling the function, this is just for practice so I have kept everything inside App.tsx. my function looks like this.
Coomonly I think array.map() can use typescript but It seems different than I thought. The HTMLElement generated with this map was not displayed on the screen.

我在调用函数时遇到错误,这只是为了练习,所以我将所有内容都保存在App.tsx中。我的函数如下所示。Coomonly我认为array.map()可以使用类型脚本,但它似乎与我想象的不同。使用此地图生成的HTMLElement未显示在屏幕上。


anyone know how to fix it?

有人知道怎么修吗?


import * as React from 'react';

export default function KeyLists() {
let keyList = ['a', 'b', 'c']
return (
<div>
{keyList && keyList.forEach((key)=>{
<li>{key}</li>
})} // I got an vscode alert.
</div>
)
}

TypeScript said.

打字稿说。


Type 'void' is not assignable to type 'ReactNode'.ts(2322)
index.d.ts(1421, 9): The expected type comes from property 'children' which is declared here on type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'

My Package.json

我的Package.json


{
"private": true,
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start",
"typecheck": "tsc",
"lint-fix": "eslint --fix './{lib,src,test}/**/*.{ts,tsx}' && prettier --write './{lib,src,test}/**/*.{ts,tsx}'"
},
"dependencies": {
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@mui/icons-material": "^5.14.8",
"@mui/material": "^5.14.8",
"classnames": "^2.3.1",
"date-fns": "^2.28.0",
"gray-matter": "^4.0.3",
"microcms-js-sdk": "^2.5.0",
"next": "latest",
"prettier": "^3.0.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"remark": "^14.0.2",
"remark-html": "^15.0.1",
"sass": "^1.66.1",
"typescript": "^4.7.4"
},
"devDependencies": {
"@types/node": "^18.0.3",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"@typescript-eslint/eslint-plugin": "^6.6.0",
"@typescript-eslint/parser": "^6.6.0",
"autoprefixer": "^10.4.7",
"eslint": "^8.49.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"postcss": "^8.4.14",
"tailwindcss": "^3.1.4"
}
}


更多回答

You're not using map...

您没有使用地图...

优秀答案推荐

Your code snippet has two problems.

您的代码片段有两个问题。



  1. You definitely must use .map() function in code like this. The reason "nothing is displayed on the screen" is that your callback function returns nothing. The correct version looks this way:


{keyList && keyList.map((key) => (
<li>{key}</li>
))}

Notice {} are replaced with ();

通知{}替换为();


As a side note: Don't forget to add special prop key to list items:

作为一个侧记:不要忘记添加特殊道具键列表项目:


{keyList && keyList.map((key) => (
<li key={key}>{key}</li>
))}


  1. Typesscript is correct emitting an error in your code as .forEach() function returns void. But React.ReactNode type is declared in such a way that it accepts null or undefined, but not void.


更多回答

That makes sense. Thanks, I solved it by your code.

这事儿可以理解。谢谢,我用你的密码解决了。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com