简体   繁体   English

如何在下拉菜单的 onChange 中更新 useState 中的数组元素状态? (我不是在这里谈论对象数组)

[英]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.

相关问题 如何在 React Hooks 中更新 object 数组中的状态 `onChange` - How do I update states `onChange` in an array of object in React Hooks 如何将 Heroes 数组更改为 res 数组? 我正在使用 react useState 钩子。 这适用于以前的项目,但不适用于这里 - How do I change the Heroes array, to the res array? I am using the react useState hook. This has worked on previous projects but not here 如何更新具有数组的useState,在该数组中我有对象,当输入值在react js中发生变化时,该对象将更新 - How to update a useState which has an array, inside this array I have objects this objects will update when the input value will change in react js 如何在 for 循环中更新本机 useState 中的对象数组? - How to update array of objects in react native useState inside for loop? 修改元素时更新 useState() 数组 REACT - update useState() array when element is modified REACT 如何使用 React JS 中的 onChange 处理程序更新对象数组? - How to update the array of objects using onChange handler in React JS? 用对象数组反应 useState - React useState with Array of objects 如何在对象数组中使用 usestate 更新 state? - How to update state with usestate in an array of objects? 如何处理数组内对象的 onChange(反应状态)? - How do I handle onChange of objects inside an array (React State)? 如何在反应中更新使用useState钩子创建的数组 - How to update array created with useState hook in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM