[英]React hooks cannot map array within JSX
I've really searched in many places of the internet and cannot for the life of me find a solution around my problem.我真的在互联网的许多地方进行了搜索,但终生无法找到解决问题的方法。
I'm unable to map through an array within my React's JSX.我无法通过我的 React 的 JSX 中的数组进行 map。 I've been able to do it in the past effortlessly but for some reason, when I decided to practice some React Hooks, things got way too tricky.
过去我可以毫不费力地做到这一点,但由于某种原因,当我决定练习一些 React Hooks 时,事情变得太棘手了。
Here is my code:这是我的代码:
import "./App.css";
import axios from "axios";
const Search = () => {
// takes information written in input field.
const handleChange = (e) => {
e.preventDefault();
const lookUp = e.target.value;
setSearchTerm(lookUp);
return lookUp;
};
// useState for searcing the term in the search box
const [searchTerm, setSearchTerm] = useState("");
// useState for submitting the form and getting a result
const [word, setWord] = useState("")
const [definitions, setDefinitions] = useState("")
const handleSubmit = (e) => {
const apiKey = "secretsecret";
const apiURL = `https://www.dictionaryapi.com/api/v3/references/thesaurus/json/${searchTerm}?key=${apiKey}`;
e.preventDefault();
axios({
method: "GET",
url: apiURL,
dataResponse: "json",
}).then((res) => {
res.data.map(wordInfo => {
const wordArray = Object.values(wordInfo)
setWord(wordInfo.hwi.hw)
if (wordInfo.hwi.hw === searchTerm) {
const defsArray = wordInfo.shortdef;
setDefinitions(defsArray)
const syns = wordInfo.meta.syns;
syns.map(synWords => {
synWords.map(wordsLol => {
})
})
}
})
});
};
return (
<div>
<div className="searchForm">
<form action="" onSubmit={handleSubmit}>
<label htmlFor="searchBox">Look up a word</label>
<input type="text" id="searchBox" onChange={handleChange} />
<input type="submit" value="Search me!" />
</form>
</div>
<div className="wordInfo">
{definitions.map(test => {
console.log(test)
})}
</div>
</div>
);
};
export default Search;
Don't mind the lame variable names, this is just my practice project.不要介意蹩脚的变量名,这只是我的练习项目。
Any input would definitely be helpful.任何输入肯定会有所帮助。 What do I seem to be doing wrong?
我似乎做错了什么?
initialize empty array here:在这里初始化空数组:
const [definitions, setDefinitions] = useState([])
Except init definitions
as array to prevent mapping a string as @Nilanka Manoj stated, when you render HTML syntax after map
, make sure you have put return
.除了将初始化
definitions
为数组以防止按照@Nilanka Manoj 所述映射字符串时,当您在 map 之后呈现map
语法时,请确保您已放置return
。 For example:例如:
<div className="wordInfo">
{definitions.map(test => {
return (
//your HTML syntax
)
})}
</div>
or you can do sth like this或者你可以这样做
<div className="wordInfo">
{definitions.map(test => (
//your HTML syntax
))}
</div>
You use searchTerm
in handleSubmit
which refer to a closure value.您在
handleSubmit
searchTerm
引用闭包值。 React won't update handleSubmit
function every time render, so you always get old value of searchTerm
which is empty when user click. React 不会在每次渲染时更新
handleSubmit
function,所以当用户点击时你总是得到searchTerm
的旧值,它是空的。
You could use useCallback and add searchTerm
as dependencies您可以使用useCallback并将
searchTerm
添加为依赖项
const handleSubmit = useCallback((e) => {
const apiKey = "secretsecret";
const apiURL = `https://www.dictionaryapi.com/api/v3/references/thesaurus/json/${searchTerm}?key=${apiKey}`;
e.preventDefault();
axios({
method: "GET",
url: apiURL,
dataResponse: "json",
}).then((res) => {
res.data.map(wordInfo => {
const wordArray = Object.values(wordInfo)
setWord(wordInfo.hwi.hw)
if (wordInfo.hwi.hw === searchTerm) {
const defsArray = wordInfo.shortdef;
setDefinitions(defsArray)
const syns = wordInfo.meta.syns;
syns.map(synWords => {
synWords.map(wordsLol => {
})
})
}
})
});
}, [searchTerm]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.