react warning cannot set state when using promises

前端 未结 7 1332
情深已故
情深已故 2020-12-19 13:40

I\'m trying to query the server to get list of nav items so I can build my menu on init. I\'ve so far managed to create a static page with 3 contents on the home page, which

7条回答
  •  我在风中等你
    2020-12-19 14:40

    Note: I have added a timeout of 2 seconds to simulate API call. so results will be seen after 2 seconds

    const jsonResponse = {
      header: {
        title: "CCP",
        description: "",
        error: true,
        message:
          "Method getMenu Not Implemented in CCP\\Controller\\UserController",
        user: "UddinS2",
        nav: {
          container: [
            {
              title: "Users",
              link: "#",
              theme: "#007c92",
              sub_modules: [
                {
                  title: "Getting Started",
                  link: "#",
                  theme: "#007c92",
                  menuitem: [
                    {
                      title: "File - CM Template v6.3",
                      link: "/library/templates/FW-LB-CommsMatrix-Template-v6.3.xls"
                    },
                    {
                      title: "File - Request Access",
                      link: "/library/docs/Requesting_Access_to_CCP.pdf"
                    },
                    {
                      title: "Video - CCP Promo",
                      link: "/video_ccp.html"
                    },
                    {
                      title: "Video - CCP Demo",
                      link: "/video_ccp_demo.html"
                    },
                    {
                      title: "Video - Request Access",
                      link: "/video_request_access.html"
                    },
                    {
                      title: "Video Tutorials",
                      link: "/video_tutorials.php"
                    }
                  ]
                },
                {
                  title: "General",
                  link: "#",
                  theme: "#007c92",
                  menuitem: [
                    {
                      title: "DC Standard Networks",
                      link: "/api/user/dcsn/format/xml"
                    },
                    {
                      title: "Find Comms Matrix Flow",
                      link: "/find_comms_matrix_flow.php"
                    },
                    {
                      title: "Find Host",
                      link: "/find_host.php"
                    },
                    {
                      title: "Find IP Network",
                      link: "/find_network.php"
                    },
                    {
                      title: "List Comms Matrices",
                      link: "/comms_matrices.php"
                    },
                    {
                      title: "My Exemption Requests",
                      link: "/api/user/exemption/requests/format/xml"
                    },
                    {
                      title: "Request Exemption",
                      link: "/request_exemption.php"
                    },
                    {
                      title: "Services, Projects, ...",
                      link: "/api/user/services/format/xml"
                    },
                    {
                      title: "View FW Objects",
                      link: "/view_network_service_objects.php"
                    },
                    {
                      title: "Create Comms Matrix",
                      link: "/create_comms_matrix.php"
                    },
                    {
                      title: "Comms Matrix Templates",
                      link: "/api/user/commsmatrix/templates"
                    },
                    {
                      title: "Upload Comms Matrix",
                      link: "/upload_comms_matrix.php"
                    },
                    {
                      title: "Verify Policy Flow",
                      link: "/verify_policy_flow.php"
                    },
                    {
                      title: "View Security Policy",
                      link: "/security_zoning_policy.php"
                    },
                    {
                      title: "View Routing Policy",
                      link: "/routing_domain_policy.php"
                    }
                  ]
                },
                {
                  title: "IP Management",
                  link: "#",
                  theme: "#007c92",
                  menuitem: [
                    {
                      title: "Adressing Types",
                      link: "/api/user/iam/addressing/types/format/xml"
                    },
                    {
                      title: "Cramer Ranges (Live)",
                      link: "/api/user/iam/cramer/ranges/format/xml"
                    },
                    {
                      title: "Cramer Spool Queue",
                      link: "/api/user/cramer/spool/queue/format/xml"
                    },
                    {
                      title: "Create Subnet",
                      link: "/iam_create_subnet.php"
                    },
                    {
                      title: "Subnet Types",
                      link: "/api/user/iam/subnet/types/format/xml"
                    },
                    {
                      title: "Zone Type Sizes",
                      link: "/api/user/security/zone/type/sizes/format/xml"
                    },
                    {
                      title: "My Network Diagrams",
                      link: "/api/user/iam/network/diagrams/format/xml"
                    },
                    {
                      title: "My Subnet Requests",
                      link: "/api/user/subnet/requests/format/xml"
                    },
                    {
                      title: "Range Assignments",
                      link: "/api/user/iam/range/assignments/format/xml"
                    },
                    {
                      title: "Upload Network Diagram",
                      link: "/iam_upload_diagram.php"
                    }
                  ]
                },
                {
                  title: "Networks",
                  link: "#",
                  theme: "#007c92",
                  menuitem: [
                    {
                      title: "Entity Networks",
                      link: "/api/user/networks/by/entity/format/xml"
                    },
                    {
                      title: "Frozen Objects",
                      link: "/api/user/frozen/objects/format/xml"
                    },
                    {
                      title: "IPBB Metadata",
                      link: "/api/user/ipbb/metadata/format/xml"
                    },
                    {
                      title: "Map Routing Zones",
                      link:
                        "/api/user/routing/domains/mapping/routingzone/format/xml"
                    },
                    {
                      title: "NITX Trace",
                      link: "/nitx_trace.php"
                    },
                    {
                      title: "Routing Domains",
                      link: "/api/user/routing/domains/format/xml"
                    },
                    {
                      title: "Routing Domain Aliases",
                      link: "/api/user/aliases/routing/domains/format/xml"
                    },
                    {
                      title: "Routing Domains ACL",
                      link: "/api/user/routing/domains/acl/format/xml"
                    },
                    {
                      title: "Routing Exemptions",
                      link: "/api/user/routing/exemptions/format/xml"
                    }
                  ]
                },
                {
                  title: "Reporting",
                  link: "#",
                  theme: "#007c92",
                  menuitem: [
                    {
                      title: "Statistics Dashboard",
                      link: "/statistics_dashboard.php"
                    },
                    {
                      title: "ECT Statistics",
                      link: "/api/user/connectivity/test/statistics"
                    },
                    {
                      title: "Exemption Requests",
                      link: "/api/user/report/exemption/requests/format/xml"
                    },
                    {
                      title: "Exemption Policies",
                      link: "/api/user/report/exemption/policies/format/xml"
                    }
                  ]
                },
                {
                  title: "Security",
                  link: "#",
                  theme: "#007c92",
                  menuitem: [
                    {
                      title: "Firewall Network Objects",
                      link: "/firewall_network_objects.php"
                    },
                    {
                      title: "Firewall Service Objects",
                      link: "/firewall_service_objects.php"
                    },
                    {
                      title: "Local Market Networks",
                      link: "/api/user/networks/opco/format/xml"
                    },
                    {
                      title: "Security Exemptions",
                      link: "/api/user/exemptions/format/xml"
                    },
                    {
                      title: "Security Zones",
                      link: "/api/user/security/zones/format/xml"
                    },
                    {
                      title: "Security Zone Aliases",
                      link: "/api/user/aliases/security/zones/format/xml"
                    },
                    {
                      title: "Network Zone Aliases",
                      link: "/api/user/aliases/network/zones/format/xml"
                    },
                    {
                      title: "Map Security Zones",
                      link: "/api/user/security/zones/mapping/seczone/format/xml"
                    },
                    {
                      title: "Map Network Zones",
                      link: "/api/user/security/zones/mapping/netzone/format/xml"
                    },
                    {
                      title: "Security Zones ACL",
                      link: "/api/user/security/zones/acl/format/xml"
                    }
                  ]
                },
                {
                  title: "Audit",
                  link: "#",
                  theme: "#007c92",
                  menuitem: [
                    {
                      title: "Firewall Network Objects",
                      link: "/api/user/audit/firewall/network/objects/format/xml"
                    },
                    {
                      title: "Firewall Service Objects",
                      link: "/api/user/audit/firewall/service/objects/format/xml"
                    },
                    {
                      title: "Security Zones",
                      link: "/api/user/audit/security/zones/format/xml"
                    },
                    {
                      title: "Security Zones ACL",
                      link: "/api/user/audit/security/zones/acl/format/xml"
                    },
                    {
                      title: "Security Zones Aliases",
                      link: "/api/user/audit/security/zone/aliases/format/xml"
                    },
                    {
                      title: "Security Zone Mappings",
                      link: "/api/user/audit/security/zone/mapping/format/xml"
                    },
                    {
                      title: "Network Zone Aliases",
                      link: "/api/user/audit/network/zone/aliases/format/xml"
                    },
                    {
                      title: "Routing Domains",
                      link: "/api/user/audit/routing/domains/format/xml"
                    },
                    {
                      title: "Routing Domains ACL",
                      link: "/api/user/audit/routing/domains/acl/format/xml"
                    },
                    {
                      title: "Routing Domain Aliases",
                      link: "/api/user/audit/routing/domain/aliases/format/xml"
                    },
                    {
                      title: "Routing Domain Mappings",
                      link: "/api/user/audit/routing/domain/mapping/format/xml"
                    }
                  ]
                }
              ]
            },
            {
              title: "Site Administration",
              link: "#",
              theme: "#007c92",
              sub_modules: [
                {
                  title: "Content Management",
                  link: "#",
                  theme: "#007c92",
                  menuitem: [
                    {
                      title: "Business Units",
                      link: "/api/user/businessunits/format/xml"
                    },
                    {
                      title: "Entities",
                      link: "/api/user/entities/format/xml"
                    },
                    {
                      title: "Locations",
                      link: "/api/user/locations/format/xml"
                    },
                    {
                      title: "Markets",
                      link: "/api/user/markets/format/xml"
                    },
                    {
                      title: "Service Domains",
                      link: "/api/user/servicedomains/format/xml"
                    }
                  ]
                },
                {
                  title: "Health Check",
                  link: "#",
                  theme: "#007c92",
                  menuitem: [
                    {
                      title: "Networks Status",
                      link: "/api/user/networks/status/format/xml"
                    }
                  ]
                },
                {
                  title: "User Management",
                  link: "#",
                  theme: "#007c92",
                  menuitem: [
                    {
                      title: "Assign Group Privilges",
                      link: "/assign_group_privs.php"
                    }
                  ]
                },
                {
                  title: "Troubleshooting",
                  link: "#",
                  theme: "#007c92",
                  menuitem: [
                    {
                      title: "Debug Comms Matrix",
                      link: "/debug_comms_matrix.php"
                    },
                    {
                      title: "Mocha Tests",
                      link: "/testrunner.php"
                    }
                  ]
                },
                {
                  title: "Super Admin",
                  link: "#",
                  theme: "#007c92",
                  menuitem: [
                    {
                      title: "Sudo Group Access",
                      link: "/sudo_group_access.php"
                    }
                  ]
                }
              ]
            }
          ]
        },
        server: 1,
        version: "17.1",
        timestamp: 1514902917
      },
      body: []
    };
    
    class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          navList: []
        };
      }
    
      componentDidMount() {
        //$.get('/api/user/get/user/method/menu/format/json')
        //.done ((response) => console.log(response));
        //.done(( response ) => this.setState({ navlist: //response } ));
        //dont have actual api hence setting state with response data mentioned in question
        setTimeout(() => {
          this.setState({
            navList: jsonResponse
          });
        }, 2000);
      }
    
      componentWillUnmount() {}
    
      render() {
        //removing header and HomeContent as definition is not there
        return (
          
    {" "}
    ); } } let SideBar = props => { const { navlist } = props; let navlistView = null; const getMenuItems = menuItemsArray => (
      {" "} {menuItemsArray.map((element, index) => (
    • {element.title}
    • ))}
    ); const getSubMenu = subMenuArray => (
    {" "} {subMenuArray.map((element, index) => (
      -- - {element.title} {
    • {getMenuItems(element.menuitem)}
    • }
    ))}
    ); const getView = () => { let navlistView = null; if (navlist.header) { const navInfo = navlist.header.nav.container; navlistView = navInfo.map((navlist, index) => (
    {navlist.title}
    {getSubMenu(navlist.sub_modules)}
    )); } return navlistView; }; return ( ); }; ReactDOM.render(, document.getElementById("app"));
    
    
    

    For first error
    Failed! TypeError: Cannot read property 'setState' of undefined
    issue : callback doesnot have correct context.
    
    
    Failed! TypeError: Cannot read property 'menuList' of undefined
    This is also context issue as getJson is called with new context.
    

    I am working on linux os , dont have IE , So I haven't checked on IE. i have used index as the keys because i don't have have any unique id. Please check what unique id you can set as keys.

提交回复
热议问题