在React中进行GET请求后动态生成导航链接时遇到问题。

huangapple go评论67阅读模式
英文:

Having issues dynamically generating nav links after a GET request in React

问题

I'm working on a React app and would like to dynamically generate a list of dropdown links based on the results of a GET request. Basically, the GET request returns an array of objects with 2 key values, cCode & cName. cCode will act as the link location and cName would be the link's proper name.

When I run the below code, the page renders, but when I click the dropdown, the list is blank. I can however confirm with a console.log that the array from the request is coming over properly and the expected data is present.

import React from 'react'
import { Nav, Navbar, NavDropdown } from 'react-bootstrap'
import { LinkContainer } from 'react-router-bootstrap'
import { useLocation } from 'react-router-dom';
import { ReactComponent as Logo } from './images/logos/unboxed/logo-black-full.svg';
import NavbarBrand from 'react-bootstrap/NavbarBrand';
import { Get } from 'react-axios';

function Banner() {
    const location = useLocation();
    return (
        <Navbar bg='light' variant='light' expand='lg' sticky='top'>
            <LinkContainer to='/'>
                <NavbarBrand bg='dark'>
                    <Logo className='.logo-svg' alt='Site Logo' height='30' />{'    '}
                    Resource Dashboard
                </NavbarBrand>
            </LinkContainer>
            <Navbar.Toggle aria-controls='basic-navbar-nav' />
            <Navbar.Collapse className='ml-5' id='basic-navbar-nav'>
                <Nav className='me-auto' activeKey={location.pathname}>
                    <NavDropdown title='Physical Sites' id='basic-nav-dropdown'>
                        <Get url="http://api.request/locations">
                            {(error, response, isLoading, axios) => {
                                if (error) {
                                    return (<div>Something bad happened: {error.message}</div>)
                                }
                                else if (isLoading) {
                                    return (<div>Loading...</div>)
                                }
                                else if (response !== null) {
                                    // console.log(response.data);
                                    const data = response.data;
                                    data.forEach(item => {
                                        <LinkContainer to={item.cCode}>
                                            <NavDropdown.Item eventKey={item.cCode}>{item.cName}</NavDropdown.Item>
                                        </LinkContainer>
                                    });
                                }
                            }}
                        </Get>
                    </NavDropdown>
                </Nav>
            </Navbar.Collapse>
        </Navbar>
    )
}
export default Banner;

Any help would be greatly appreciated.

英文:

I'm working on a React app and would like to dynamically generate a list of dropdown links based on the results of a GET request.
Basically, the GET request returns an array of objects with 2 key values, cCode & cName. cCode will act as the link location and cName would be the link's proper name.

When I run the below code, the page renders, but when I click the dropdown, the list is blank. I can however confirm with a console.log that the array from the request is coming over properly and the expected data is present.

import React from &#39;react&#39;
import { Nav, Navbar, NavDropdown } from &#39;react-bootstrap&#39;
import { LinkContainer } from &#39;react-router-bootstrap&#39;
import { useLocation } from &#39;react-router-dom&#39;;
import { ReactComponent as Logo } from &#39;./images/logos/unboxed/logo-black-full.svg&#39;;
import NavbarBrand from &#39;react-bootstrap/NavbarBrand&#39;;
import { Get } from &#39;react-axios&#39;;
function Banner() {
const location = useLocation();
return (
&lt;Navbar bg=&#39;light&#39; variant=&#39;light&#39; expand=&#39;lg&#39; sticky=&#39;top&#39;&gt;
&lt;LinkContainer to=&#39;/&#39;&gt;
&lt;NavbarBrand bg=&#39;dark&#39;&gt;
&lt;Logo className=&#39;.logo-svg&#39; alt=&#39;Site Logo&#39; height=&#39;30&#39; /&gt;{&#39;    &#39;}
Resource Dashboard
&lt;/NavbarBrand&gt;
&lt;/LinkContainer&gt;
&lt;Navbar.Toggle aria-controls=&#39;basic-navbar-nav&#39; /&gt;
&lt;Navbar.Collapse className=&#39;ml-5&#39; id=&#39;basic-navbar-nav&#39;&gt;
&lt;Nav className=&#39;me-auto&#39; activeKey={location.pathname}&gt;
&lt;NavDropdown title=&#39;Physical Sites&#39; id=&#39;basic-nav-dropdown&#39;&gt;
&lt;Get url=&quot;http://api.request/locations&quot;&gt;
{(error, response, isLoading, axios) =&gt; {
if (error) {
return (&lt;div&gt;Something bad happened: {error.message}&lt;/div&gt;)
}
else if (isLoading) {
return (&lt;div&gt;Loading...&lt;/div&gt;)
}
else if (response !== null) {
// console.log(response.data);
const data = response.data;
data.forEach(item =&gt; {
&lt;LinkContainer to={item.cCode}&gt;
&lt;NavDropdown.Item eventKey={item.cCode}&gt;{item.cName}&lt;/NavDropdown.Item&gt;
&lt;/LinkContainer&gt;
});
}
}}
&lt;/Get&gt;
&lt;/NavDropdown&gt;
&lt;/Nav&gt;
&lt;/Navbar.Collapse&gt;
&lt;/Navbar&gt;
)
}
export default Banner;

Any help would be greatly appreciated.

答案1

得分: 0

有3个问题:

  1. 最后一个else if中没有返回任何内容

  2. 应该使用map而不是forEach,因为forEach不返回值

  3. map中使用括号而不是花括号,以返回值

所以,你应该将以下代码:

data.forEach(item =&gt; {&lt;...&gt;})

替换为:

return data.map(item =&gt; (&lt;...&gt;))
英文:

There are 3 problems:

  1. You don't return anything from last else if

  2. You should use map instead of forEach, because forEach doesn't return values

  3. Use parentheses inside map instead of curly brackets, to return values

So, you should replace

data.forEach(item =&gt; {&lt;...&gt;})

With

return data.map(item =&gt; (&lt;...&gt;))

huangapple
  • 本文由 发表于 2023年2月24日 09:06:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定