英文:
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 '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.
答案1
得分: 0
有3个问题:
-
最后一个
else if
中没有返回任何内容 -
应该使用
map
而不是forEach
,因为forEach
不返回值 -
在
map
中使用括号而不是花括号,以返回值
所以,你应该将以下代码:
data.forEach(item => {<...>})
替换为:
return data.map(item => (<...>))
英文:
There are 3 problems:
-
You don't return anything from last
else if
-
You should use
map
instead offorEach
, becauseforEach
doesn't return values -
Use parentheses inside
map
instead of curly brackets, to return values
So, you should replace
data.forEach(item => {<...>})
With
return data.map(item => (<...>))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论