代码之家  ›  专栏  ›  技术社区  ›  Darren

.filter(…)。则不是componentDidMount中的函数

  •  -3
  • Darren  · 技术社区  · 7 年前

    我正在努力 filter 内部阵列 componentDidMount then setState 在…上 propertyID

    componentDidMount() {
      const pathname = "/path2";
      const properties = this.props.properties;
      properties
        .edges
        .filter(node => {
          return `/${node.slug}` === pathname;
        })
        .then(node =>
          this.setState({ propertyID: node.information.propertyID })
        );
    }
    

    未捕获类型错误:properties.filter(…)。then不是函数 在ProxyComponent.componentDidMount处

    this.props.properties

    {
      "edges": [
        {
          "node": {
            "id": "121323",
            "slug": "path1",
            "information": {
              "propertyID": "property1"
            }
          }
        },
        {
          "node": {
            "id": "332342",
            "slug": "path2",
            "information": {
              "propertyID": "property2"
            }
          }
        },
        {
          "node": {
            "id": "123234",
            "slug": "path3",
            "information": {
              "propertyID": "property3"
            }
          }
        }
      ]
    }
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Nitish Narang    7 年前

    你必须改变过滤线如下,它将工作。在Filter函数中,请注意我是如何使用{}表示法中的节点对象的

    {
          "node": {
            "id": "121323",
            "slug": "path1",
            "information": {
              "propertyID": "property1"
            }
          }
        }
    

    但您需要这个对象的“节点”,这就是为什么在filter函数中添加{}。

    var properties = {
      "edges": [
        {
          "node": {
            "id": "121323",
            "slug": "path1",
            "information": {
              "propertyID": "property1"
            }
          }
        },
        {
          "node": {
            "id": "332342",
            "slug": "path2",
            "information": {
              "propertyID": "property2"
            }
          }
        },
        {
          "node": {
            "id": "123234",
            "slug": "path3",
            "information": {
              "propertyID": "property3"
            }
          }
        }
      ]
    }
    
    console.log(properties
        .edges
        .filter(({ node }) => `/${node.slug}` === '/path1'))
        2
  •  0
  •   lazreg87    7 年前

    componentDidMount() {
      const pathname = "/path2";
      const properties = this.props.properties;
      const filteredArray = properties
        .edges
        .filter(node => {
          return `/${node.slug}` === pathname;
        });
    
      filteredArray.forEach((node) => {
        // Computed property, so each node gets set separately in state
        this.setState({ [node.information.propertyID]: node.information.propertyID })
      });
    }
    

    Array.filter reference

        3
  •  0
  •   Shota Tamura    7 年前

    componentDidMount() {
      const pathname = '/path2';
      const properties = this.props.properties;
    
      const node = properties.edges.filter(node => {
        return `/${node.slug}` === pathname;
      });
    
      this.setState({ propertyID: node.information.propertyID });
    }