[英]How to update the array element states in useState in react in onChange of dropdown menu? (I am not talking about array of objects here)
I want to update the states
each time when we toggle to dropdown.每次切换到下拉菜单时,我都想更新
states
。 means saves the state and update it and again like this... I am trying the following code:意味着保存 state 并像这样更新它......我正在尝试以下代码:
My React
code:我的
React
代码:
var [state, setState] = useState({
page: 0,
username: "Hey Baby!",
filter: ["search", "story", 0],
result: 0,
processTime: 0
});
var { page, username, filter, result, processTime } = state;
var handleChange = name => e => {
if (e.target.value == "popularity") {
setState({
...state, filter: ["search", filter[1], filter[2]]
});
urls =
"search~~"+
filter[1] +
"~0~" +
filter[2];
}
if (e.target.value == "date") {
setState({
...state, filter: ["search_by_date", filter[1], filter[2]]
});
urls =
"search_by_date~~"+
filter[1] +
"~0~" +
filter[2];
}
if (e.target.value == "stories") {
setState({
...state, filter: [filter[0], "story", filter[2]]
});
urls =
filter[0]+"~~story~0~" +
filter[2];
}
if (e.target.value == "comments") {
setState({
...state, filter: [filter[0], "comment", filter[2]]
});
urls =
filter[0]+"~~comment~0~" +
filter[2];
}
axios
.get(`${process.env.REACT_APP_API}/posts/${urls}`)
.then(function(response) {
setState({
result: response.data.datas.nbHits,
processTime: response.data.datas.processingTimeMS
});
setPosts(response.data.datas);
console.log(response.data.datas);
})
.catch(function(error) {
console.log(error);
});
return (
<div>
<div className="bg-white">
<span>
search{" "}
<select onChange={handleChange('filter')}>
<option value="stories">stories</option>
<option value="comments">comments</option>
</select>
</span>
<span>
{" "}
by{" "}
<select onChange={handleChange('filter')}>
<option value="popularity">popularity</option>
<option value="date">date</option>
</select>
</span>
</div>
</div>
But the problem is that once changed the select menu, it loads and call the api and the page renders.但问题是,一旦更改了 select 菜单,它就会加载并调用 api 并呈现页面。 But again the second time when I change the dropdown it throws the following error:
但是当我第二次更改下拉菜单时,它再次抛出以下错误:
TypeError: Cannot read property '1' of undefined
(anonymous function)
C:/Users/SSSYSSY/Desktop/ServerGuy/client/src/App.js:43
40 | }
41 | if (e.target.value == "date") {
42 | setState({
> 43 | ...state, filter: ["search_by_date", filter[1], filter[2]]
| ^ 44 | });
PS I don't understand if the filter arrays element states getting updated or not. PS我不明白过滤器 arrays 元素状态是否正在更新。 The first time when I change the select menu options it works.
当我第一次更改 select 菜单选项时,它起作用了。 but again when I change to different option it throws the above like error.
但是当我更改为不同的选项时,它又会抛出上述错误。
setState({
result: response.data.datas.nbHits,
processTime: response.data.datas.processingTimeMS
});
I think you're updating the state incorrectly.我认为您错误地更新了 state。 You should update the state like this
你应该像这样更新 state
setState({
...state,
result: response.data.datas.nbHits,
processTime: response.data.datas.processingTimeMS
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.