简体   繁体   English

无法针对对象数组设置状态:React JS

[英]Not able to setstate with respect to array of objects: React JS

I am trying to map few fields in array of objects, here in this case it is fieldnames and sort order. 我试图映射对象数组中的几个字段,在这种情况下,这是字段名称和排序顺序。

I am trying to achieve server side sorting functionality where in the server takes the field name and sort type whenever I click on a field. 我试图实现服务器端排序功能,每当我单击一个字段时,服务器中的服务器都会采用字段名称和排序类型。 I just need to map the field names with the sort type(ASCENDING or DESCENDING) . 我只需要使用排序类型(ASCENDING或DESCENDING)来映射字段名称。

I have written a sample where I am maintaining a sample array of objects with type. 我编写了一个示例,其中维护了一个带有类型的对象的示例数组。 And on click of that column need I need to decide its sorting order 在单击该列时,我需要确定其排序顺序

Can someone help here , Just need to achieve the tagging of sort order with the field name 有人可以帮忙吗,只需要用字段名称来实现排序的标签

Sandbox: https://codesandbox.io/s/hopeful-wescoff-08x8x 沙箱: https//codesandbox.io/s/hopeful-wescoff-08x8x

import React from "react";
import ReactDOM from "react-dom";
import { render } from "react-dom";

interface IState {
  sorting: any;
}
interface IProps {}

export default class App extends React.Component<IProps, IState> {
  constructor(props: any) {
    super(props);
    this.state = {
      sorting: [{ firstName: "" }, { lastName: "" }]
    };
  }

  sortHandler = name => {
    const sorting = Object.keys(this.state.sorting).reduce((obj, key) => {
      if (key === name) {
        obj[key] = this.state.sorting[key] === "ASC" ? "DESC" : "ASC";
      } else {
        obj[key] = "";
      }
      return obj;
    }, {});

    this.setState({ sorting }, () => console.log(this.state.sorting));
  };

  render() {
    return (
      <div>
        <span onclick={this.sortHandler("firstName")}> FirstName</span>
        <span onclick={this.sortHandler("lastName")}> LastName</span>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);



Your click-handlers get executed immediately on render and with the logic you have constructed this will cause the "Maximum update depth exceeded" error. 单击处理程序将在render立即执行,并且使用您已构建的逻辑,这将导致“超出最大更新深度”错误。

Pass an anonymous function that will call your sortHandler instead. 传递一个匿名函数,该函数将改为调用您的sortHandler This will make it so the sortHandler only gets executed when the user clicks the span: 这样可以使sortHandler仅在用户单击span时执行:

  render() {
    return (
      <div>
        <span onclick={() => this.sortHandler("firstName")}> FirstName</span>
        <span onclick={() => this.sortHandler("lastName")}> LastName</span>
      </div>
    );
  }

See sandbox for example on how to sort by fieldnames: https://codesandbox.io/s/vigilant-haslett-c2z3f 有关如何按字段名称排序的示例,请参见沙箱: https : //codesandbox.io/s/vigilant-haslett-c2z3f

 import React from "react"; import ReactDOM from "react-dom"; //import { render } from "react-dom"; interface IState { sorting: any; } interface IProps {} export default class App extends React.Component<IProps, IState> { constructor(props: any) { super(props); this.state = { sorting: [{ firstName: "" }, { lastName: "" }] }; } sortHandler = (name => { const sorting = Object.keys(this.state.sorting).reduce((obj, key) => { if (key === name) { obj[key] = this.state.sorting[key] === "ASC" ? "DESC" : "ASC"; } else { obj[key] = ""; } return obj; }, {}); this.setState({ sorting }, () => console.log(this.state.sorting)); }); render() { return ( <div> <span onclick={() => this.sortHandler("firstName")}> FirstName</span> <span onclick={() => this.sortHandler("lastName")}> LastName</span> </div> ); } } const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement); 

Few Highlighted issue on your code, please do not import the code if you're not using which can consume memory. 代码中的突出问题很少,如果不使用会消耗内存的代码,请不要导入。

Arrow function dedicated to using the function on the scope. 箭头功能专用于在示波器上使用该功能。 Please check the scope of the variable if the variable required bind with scope 如果所需变量与范围绑定,请检查变量的范围

    const sorting = Object.keys(this.state.sorting).reduce((obj, key) => {

Please make sure the calling function also requires the scope this to the function. 请确保调用函数也需要此函数的作用域。 I hope your problem is solved by the initial solution. 希望您的问题能通过最初的解决方案得到解决。 its just additional notes 它只是补充说明

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

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